Summarize Contents of Array

Actionscript:
  1. var a:Array = [true, true, true, false, false, true, true, true, false];
  2.  
  3. var counter:int = 0;
  4. var prev:Boolean;
  5. var summary:Array = [];
  6. for (var i:int = 1; i<a.length; i++){
  7.     prev = a[i - 1]
  8.     counter++;
  9.     if (prev != a[i]){
  10.         if (prev){
  11.             summary.push("true: "+ counter);
  12.         }else{
  13.             summary.push("false: "+ counter);
  14.         }
  15.         counter = 0;
  16.     }
  17. }
  18. summary.push(a[i-1].toString()+": "+ (counter+1));
  19.  
  20. trace(summary);
  21.  
  22. /** outputs:
  23. true: 3,false: 2,true: 3,false: 1
  24. */

This is a handy way to summarize the contents of an array of boolean values.

This entry was posted in arrays, misc and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

8 Comments

  1. Posted July 25, 2009 at 9:41 am | Permalink

    I’m having a problem with QuickBox2D. If I open up the QuickBox2D.as file and add my own function in there:

    public function tester():void {
    trace(”test”);
    }

    then i test it with this code:
    import com.actionsnippet.qbox.*;
    var sim:QuickBox2D = new QuickBox2D(this,{debug:true});
    sim.createStageWalls();
    sim.start();
    sim.tester();

    I get this error:
    1061: Call to a possibly undefined method tester through a reference with static type com.actionsnippet.qbox:QuickBox2D.

    I’mtrying to make an explosion function, which gave that same error, so i added that test function to see if it would work, and it didn’t so, i don’t know…

    Anyway, It may be because I’ve done a bit of modification here and there. So, I’m going to download alpha 007 again, and see if it works

    Thanks,
    Thomas Francis

  2. Posted July 25, 2009 at 9:47 am | Permalink

    Well, apparently it works fine with the new alpha 107 i downloaded… time to see what i did to mess up the other one…

  3. Posted July 25, 2009 at 10:04 am | Permalink

    Wow, this is sad. It turns out that I was editing a version of the QuickBox2D.as file that was in a different project folder…

  4. Posted July 25, 2009 at 6:38 pm | Permalink

    saw your first comment this morning and didn’t have time to respond… but actually that thought crossed my mind… I’ve done similar things in the past :)

  5. Posted July 25, 2009 at 8:15 pm | Permalink

    haha, yeah, it happens.

    Well, got it all sorted out, check it out:
    http://www.theorangeday.com/content.php?type=flash&file=/Content/Demo/explosiontest.swf

    The only problem is that when the boxes get into the right corner, i can’t apply impulses…

    This is function I added to QuickBox2D.as:
    public function EXPLODE(pos:b2Vec2, Radius:Number, Force:Number):void {
    var aabb:b2AABB = new b2AABB();
    aabb.lowerBound.Set(pos.x - Radius, pos.x - Radius);
    aabb.upperBound.Set(pos.y + Radius, pos.y + Radius);
    var k_maxCount:int=512;
    var shapes:Array = new Array();
    var count:int=w.Query(aabb,shapes,k_maxCount);
    var Body:b2Body=null;
    var HitVector:b2Vec2 = new b2Vec2(0,0);
    var BodyPos:b2Vec2 = new b2Vec2(0,0);
    var HitForce:Number = 0;
    var Distance:Number = 0;
    for (var i:int = 0; i < count; ++i) {
    Body = shapes[i].GetBody();
    Body.WakeUp();
    BodyPos = Body.GetWorldCenter();
    //HitVector = BodyPos.Subract(pos);
    HitVector.x = BodyPos.x-pos.x;
    HitVector.y = BodyPos.y-pos.y;
    Distance = HitVector.Normalize(); //Makes a 1 unit length vector from HitVector, while getting the length.
    if ((Body.IsDynamic()) && (Distance<=Radius))
    {
    HitForce=(Radius-Distance)*Force; //TODO: This is linear, but that’s not realistic.
    HitVector.x*=HitForce;
    HitVector.y*=HitForce;
    //HitVector.Multiply(HitForce);
    Body.ApplyImpulse(HitVector, Body.GetWorldCenter());
    }
    }
    }

    Couldn’t get the b2Vec2 methods to work, so i had to do it manually…

    And here is the timeline code:
    import com.actionsnippet.qbox.*;
    import Box2D.Common.Math.*;

    var sim:QuickBox2D = new QuickBox2D(this,{debug:true});
    sim.createStageWalls();

    sim.addBox({x:3, y:2});
    sim.addBox({x:3, y:3});
    sim.addBox({x:3, y:4});
    var explosm:QuickObject = sim.addBox({x:4.5,y:3});

    sim.start();

    stage.addEventListener(MouseEvent.CLICK, doIt);

    function doIt(e:MouseEvent):void {
    sim.EXPLODE(new b2Vec2(mouseX/30, mouseY/30), 4, 15);
    }

    -Thomas

  6. Posted July 26, 2009 at 12:09 pm | Permalink

    Hey Thomas…. that looks cool, however I think it can be significantly simplified… check this out:

    import com.actionsnippet.qbox.*;
    import Box2D.Common.Math.*;

    var sim:QuickBox2D = new QuickBox2D(this);

    sim.createStageWalls();

    var boxNum:int = 5;
    var boxes:Array = [];
    var lookup:Dictionary = new Dictionary(true);
    for (var i:int = 0; i boxes[i] = sim.addBox({x:5, y:3 + i, width:1, height:1, angularDamping:1});
    // use userData to be able to lookup the corresponding QuickObject
    lookup[boxes[i].userData] = boxes[i];
    boxes[i].userData.addEventListener(MouseEvent.CLICK, onApplyImpulse);
    boxes[i].userData.buttonMode = true;
    }

    var vec:b2Vec2 = new b2Vec2();
    var mouseVec:b2Vec2 = new b2Vec2();

    function onApplyImpulse(evt:Event):void{
    var box:QuickObject = lookup[evt.currentTarget];
    vec.x = Math.random() *20 - 10;
    vec.y = Math.random() * - 20 - 5;
    mouseVec.x = mouseX / 30;
    mouseVec.y = mouseY / 30;
    box.body.ApplyImpulse(vec, mouseVec);
    }

    sim.start();

  7. Posted July 26, 2009 at 12:11 pm | Permalink

    i only skimmed your code, but maybe my example will give you some ideas of how to simplify yours… pretty sure you don’t need to do any query stuff for what your trying to do…

  8. Posted July 26, 2009 at 12:14 pm | Permalink

    on second skim of your code, I guess that mine doesn’t do exactly what yours does… but maybe it will still give you some ideas…

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*