how do you use a for loop to clear the draw method of multiple mcs?
i guess what i need to know is how to make the for loop increment through the different instance names. they are in the format curve1, curve2… curve3.
cheers
Dave
how do you use a for loop to clear the draw method of multiple mcs?
i guess what i need to know is how to make the for loop increment through the different instance names. they are in the format curve1, curve2… curve3.
cheers
Dave
Something like this?
var i = 1;
var tmp = null;
while(tmp = this["curve" + i])
{
tmp.clear();
i++;
}
This code is assumed to run with the timeline which the movieclips are on. If that is a different timeline than which the script runs on replace “this” in the code with the path to that timeline.
/Mirandir
thanks
I just wanted to point out that I like what you did with the while loop here Mirandir… It’s not really an issue with your project at the moment I’m sure, but I just wanted to get it out that it’s good habit to make sure that the memory you use throughout your application is freed when no longer needed.
The way that Garbage Collection (GC) works, whether you are using FlashPlayer 7 and Below, or FlashPlayer 8 (which implements a different GC model), is through referencing. In actionscript everything is an object, when you create a new instance of any class an anonymous object is created on the Heap (A chunk of memory that is dedicated to your objects), and the reference to that object is returned and set to the variable you place the object in.
Essentially the way that the GC works in both model’s is this, as long as a reference to an object exists, that object will never be released and the memory never freed.
For instance let’s say you had a while loop…
var i=100;
while (i)
{
trace(i);
i--;
}
trace(i); // 0
You’ll find that i will exist throughout the life of your application (if not freed of course). Now this will never be a problem with simple loops like this, but as your applications get bigger and you start dealing with a few thousand lines of code, you will actually notice a performance increase when you are cleaning up after yourself. You can be sure that the memory is freed in a few ways.
The first is by encapsulating specific functionality into a function. This works because when using the var keyword the variable only exists within that block of code…essentially between the openening { and closing } which is used to denote a block of code, the variable is only used within the function… memory is allocated on the call stack, and not on the heap. As the call stack unwinds the memory associated is freed. So if you create a function and call it… when the function is done executing the associated variables do not exist.
iterate();
trace(i); // undefined
function iterate() {
var i=100;
while (i) {
trace(i);
i--;
}
}
You can also remove references manually using the delete operator… this only removes the reference specified… so if you have more than one reference to a single object, you have to delete every reference in order for the memory to be freed… here is an example going along with what we are saying…
var i=100;
while (i)
{
trace(i);
i--;
}
delete i;
trace( i ); // undefined
I know this might have seemed a little exhaustive and maybe even overkill, but it’s important that you are cleaning up after yourself, and I figured I’d just take this chance to mention it… never hurts…
If anyone is more interested in how the two versions of the GC work in the Macromedia Flash Player (7&below - 8) just ask, I can explain it for anyone who is interested, it is actually useful information and can give you an understanding of why large applications alot of times will begin to slow down. Alot of that can be prevented.
I hope everyone understands why I decided to talk about this here. Take Care.
_Michael
:: Copyright KIRUPA 2024 //--