Skip to main content
screeen
Inspiring
February 24, 2012
Question

Create a Counter in AS3.0: to count the variables added to the stage.

  • February 24, 2012
  • 2 replies
  • 6533 views

I'd like to make a counter in AS3.0, in a document class file, for a Flash project, whereby the counter counts the number of movie clips added to the stage.

The current document class is set up with a number of timers, an "ouber"timer, which activates three additional timers every 500000 milisecs, while the additional timers add different movie clips to the stage, each adds a different amount and at a different rate.

And, i'd like to add some code to this AS file, to tell flash to count how many mcies  and if 20 is the sum of the mcies on the stage, stop the timers and remove the mcies.

Please give me advice how to do this.

thanks:)

This topic has been closed for replies.

2 replies

sinious
Legend
March 2, 2012

When I create a lot of objects on the screen of similar 'meaning' (banners, news items, whatever) I track similar items by storing a reference to each of them in an array. If you're tracking lots of different types of things then I make an array of arrays and hold references to separate items in each.

I would use a Vector if you know the type will be constant for even more speed with lots of objects.

e.g. ref array

var references:Array = new Array();

references[0] = new Array(); // for stars

references[1] = new Array(); // for clouds

references[2] = new Array(); // for.. um.. planes?

function myTimerWantsAStar():void

{

    // creating a star

    references[0].push(createMeAStarAndReturnAReference());

}

function myTimerWantsACloud():void

{

    // creating a cloud

    references[1].push(createMeACloudAndReturnAReference());

}

function myTimerWantsAPlane():void

{

    // creating a plane

    references[2].push(createMeAPlaneAndReturnAReference());

}

As your timers add instances you just push them into their respective array. Then you easily can find out how many stars you have with references[0].length. Or you can aggregate them all with a loop over the array that tallies the .length property like var totalNum:int = 0; for (var i:int = 0; i < references.length; i++) { totalNum += references.length; }. No real rocket science.

You can also easily access each item for any purpose, such as adjusting it, removing it, etc by iterating the array.

screeen
screeenAuthor
Inspiring
March 3, 2012

kglad - i will trace this.stage in BaseClass.as and let you know.

Sinious, your offer is outrageously cool:) I'd have never thought of it (too beginner), will integrate it into either  BaseClass.as or DocumentClass.as

I need a bit more from you though, i dont understand:  aggregate them all references with a loop over the array that sums the length.

If i create the variable totalNum, and the loop tallies the references, what am tracing? Is it the length property, as such:

var totalNum:int = 0;

for (var i:int = 0; i < references.length; i++)

{

     totalNum += references.length;

}

trace(references.length);

kglad
Community Expert
Community Expert
February 24, 2012

for your purposes, you don't need to create one.  flash already has a counter property you can use, numChildren:

stage.numChildren

that doesn't distinguish movieclips from other displayobjects but it doesn't appear that matters for your needs.

screeen
screeenAuthor
Inspiring
February 24, 2012

oh cool!:)

kglad
Community Expert
Community Expert
February 24, 2012

you're welcome.

p.s.  please mark helpful/correct responses.