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
  • 6525 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

Not that simple.

have put a trace(trace(stage.numChildren); in the functions that addChild(varialbes) to the stage but am getting the number 1 repeating over and over, when in fact i need the sum.

SO, how do you tell flash to count by adding up the number of mcies added to the stage and giving a sum?

kglad
Community Expert
Community Expert
March 2, 2012

kgald- not sure what u're saying. BaseClass is the name of the base of the movie clip. so i assume, yes, it is in the display list.

Sinious, i am not creating mcies via an array, rather, considering that the mcies are in the library, their instances are creatd by a timer. (If i used an array or for()loop they'd all appear at once, not over time).

I should veyr much like to know how to create a variable that tracks the toltal number of instances created by the class.


in BaseClass, use:

trace(this.stage);

where you want your counter code.  if it returns null, BaseClass is not in the display list when your counter code is executing.