Skip navigation
Currently Being Moderated

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

Feb 24, 2012 5:28 AM

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:)

 
Replies
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 24, 2012 5:30 AM   in reply to minimalcomfort

    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.

     
    |
    Mark as:
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 24, 2012 5:32 AM   in reply to minimalcomfort

    you're welcome.

     

    p.s.  please mark helpful/correct responses.

     
    |
    Mark as:
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 24, 2012 5:53 AM   in reply to minimalcomfort

    then you're not adding objects to the stage.

     

    use the numChildren property applied to the SAME displayobjectcontainer to which you're adding children.

     
    |
    Mark as:
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 25, 2012 5:24 AM   in reply to minimalcomfort

    each trace shows how many children your container has if you're using something like:

     

    trace(yourcontainer.numChildren)

     
    |
    Mark as:
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 26, 2012 6:57 AM   in reply to minimalcomfort

    what is it that you want to count?  all the movieclips in containerZ etc?  the total movieclips in all the containers? 

     

    if the later, to what are your containers added?  ie, what's containerZ.parent,containerY.parent etc?

     
    |
    Mark as:
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 27, 2012 6:34 AM   in reply to minimalcomfort

    if you want to count all the movieclips in all the containers, use:

     

    this.numChildren

     

    in your document class.  if you're outside of your document class you can use:

     

    containerY.parent.numChildren, containerQ.parent.numChildren etc

     
    |
    Mark as:
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 1, 2012 3:00 PM   in reply to minimalcomfort

    is BaseClass in your display list?  if so, it can access anything else in your display list.  if not, then you must a reference to some display list object to BaseClass.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 1, 2012 8:09 PM   in reply to kglad

    Bottom line, references are cheap on memory.

     

    Every time you 'create' something you should be incrementing a value in an array that tracks references.

     

    e.g. mc1 adds 3 references therefore references[0] = 3..

     

    mc2 adds 10 references therefore references[1] = 10..

     

    mc.....9999999 adds 5 references therefore references[9999999] = 5..

     

    Just for() loop the references Array and tally the numbers and you'll know the number of objects instantiated.

     

    Tracking the number of objects instantiated inspires you to create a variable that tracks all these references so you can do things like find out the total number of instances of some class created.

     
    |
    Mark as:
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 2, 2012 6:13 AM   in reply to minimalcomfort

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 2, 2012 8:58 AM   in reply to minimalcomfort

    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[i].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.

     
    |
    Mark as:
  • kglad
    72,213 posts
    Jul 21, 2002
    Currently Being Moderated
    Mar 3, 2012 6:51 AM   in reply to minimalcomfort

    in your BaseClass constructor, use:

     

    this.addEventListener(Event.ADDED_TO_STAGE,inti);

     

    //and outside the constructor in BaseClass add:

     

    private function init(e:Event):void{

    // add your counting code here

    }

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points