Copy link to clipboard
Copied
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:)
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
oh cool!:)
Copy link to clipboard
Copied
you're welcome.
p.s. please mark helpful/correct responses.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
then you're not adding objects to the stage.
use the numChildren property applied to the SAME displayobjectcontainer to which you're adding children.
Copy link to clipboard
Copied
Yes, u are correct (as usual), the mcies are added to a container, but this is hardly a counter, seems more of an index, and there are repeating numbers...so i cant figure out even how to count them.
I was wondering if you or any others can help. see the trace below.
1
1
1
2
2
3
3
4
4
5
5
5
5
5
5
5
5
createTimers is hereby traced
2
1
1
2
2
3
3
4
4
4
4
3
3
3
3
4
4
createTimers is hereby traced
3
2
2
2
2
2
2
3
3
4
4
3
3
4
4
5
5
createTimers is hereby traced
4
1
1
2
2
3
3
4
4
4
4
5
5
6
6
7
7
createTimers is hereby traced
5
1
1
2
2
2
2
3
3
3
3
4
4
5
5
6
6
Copy link to clipboard
Copied
each trace shows how many children your container has if you're using something like:
trace(yourcontainer.numChildren)
Copy link to clipboard
Copied
Kglad I appreciate your tying to help, but my question remains unanswered because, in fact, the trace statement above, used in my code, does not reflect the amount of mcies in the container.
I have an ouberTimer managing three additional timers, as mentioned, and each timer creates one or more containers to which are added the differing mcies at differing rates and amounts.
So, each container, contains one kind of mc.
Timer1 is set to add a single mc of type x to containerX.
Timer2 creates 2 containers (containerZ and containerY) and adds 8 mcies of type z to containerZ and 8 mcies of type y to containerY.
Timer3 creates the container for mc-type Q - containerQ, to which is added one mc of type Q.
Each container is traced for its numChildren, and i am getting this list below, which is far from a counting mechanism. i dont understand it..........
If you can help understand it or produce some code that will actually do the counting, i'd appreciated it. If not, thanks for providing answers that were helpful, considering that i'm at a beginner level and any help helps:)
1
1
1
2
2
3
3
4
3
5
4
5
4
6
4
7
4
8
3
9
4
9
3
10
3
10
4
1
11
5
11
6
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
no, the containers are not inheriting each other, i.e. each container is added to the stage (or to the document class - not sure) WITHOUT containing the other containers; containers contain only mcies.
The ouberTimer container is in fact not containing anything and is gone, was unnecessary for the timer functionality. thanks for that. One less object to worry about.
The containerX.parent traces this:
[object DocumentClass]
so i assume, containerX is added to the stage. Or is it to the class?
and Yes, i am trying to count all the mcies in total in all three containers, keeping in mind that the containers are added to the stage simultaneously, and each one keeps accumulating mcies.
It would be good to be able to know which mc it is that's added at any given moment to any give container.
something like - getChild(xTypeMc)ByName.containerX.and_countKids
For example, when mc1 is added to container1 the trace outputs three ones, because at the same time i'm tracing the mcies added to containers 2 and 3.
but this - trace(container1.numChildren + container2.numChildren + container3.numChildren);
also gives me a list:
3
4
4
4
6
7
and it's unclear from the list if we're looking at the sum of 3+4+4+4+6+7 or if 7 is the sum of the mcies in the total number of containers.
I think the numChildren is doing a count for each container separately, when what i need is for the counter to give me a sum of the total number of mcies that have been added to the total number of container.
The point of all this, BTW, is to further my optimizing possibilities, to know which mc is added - x, y or z, etc - in order to remove that one from the display list completely, or alternatively, to let it stay but stop the ouberTimer (from adding more), so that i dont mess up my users' system by overloading mcies.
Any ideas.
thanks.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
NO, containerY is undefined outside the document class.
In the last few days I've tried numerous combination and still am failing
to reference containerY from outside the DocumentClass.as which is where it
is created; i am tring to do so in the BaseClass.as of an mc (that's in the
fla that has DocumentClass.as).
So, what i have going is an fla with a document class, some mcies in the
library, and some of those mcies have a mutual BaseClass.as (via Linkage in
the library symbol), and am failing to countKids of containerY in
BaseClass.as.
sorry to trouble and thanks for all your help throughout.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
kglad, hi again. just to say, i'm getting "null" (at trace(this.stage);) to indicate BaseClass.as is not in the display list of the fla. Not sure how to get it there.
Copy link to clipboard
Copied
sinious, am trying to use your suggesting, but failing. In fact, am unclear about how the references arrays are meant to
createMeAStarAndReturnAReference()
my code looks like this: starts with the import statements, the DocumentClass extends MovieClip, the constructor creates and addChilds the containers (for the various mcies), and it inititiatesTimers().
Below that are the initializeTimerFunctions():void {
timerMaker = new Timer(40000);//this is a good rate for oubertimer.
timerMaker.addEventListener(TimerEvent.TIMER, createTimers);
timerMaker.start();
}
and, the timer event functions - createTimers(event:TimerEvent):void and createMcies, which addChild(mcies);
So, i tried adding these arrays, which i understood you to suggest are reusing the createMcies functions, or allocating them to an array.
but am getting errors, and particularly, am unclear about the 'ReturnAReference' bit in your suggestion.
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);