Summarize Contents of Array

var a:Array = [true, true, true, false, false, true, true, true, false];

var counter:int = 0;
var prev:Boolean;
var summary:Array = [];
for (var i:int = 1; i<a.length; i++){
	prev = a[i - 1]
	counter++;
	if (prev != a[i]){
		if (prev){
			summary.push("true: "+ counter);
		}else{
			summary.push("false: "+ counter);
		}
		counter = 0;
	}
}
summary.push(a[i-1].toString()+": "+ (counter+1));

trace(summary);

/** outputs:
true: 3,false: 2,true: 3,false: 1
*/

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

8 comments

  1. 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. 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 ๐Ÿ™‚

  3. 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

  4. 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

  5. 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…

  6. 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…

Leave a Reply

Your email address will not be published. Required fields are marked *