Mouse Trails
Free Your Processor
Everything is working great for now, but you're going to tax the computer processor once n gets to be a large number. Consider the way the for loop is always starting from 0 (n=0) even though movieClip trail0 may already be removed. In fact, after some time the loop may be checking quite a few movieClips that don't "exist" anymore. This is going to start bogging down the processor.
So let's find a way to fix that. We're most certainly going to need to initialize n equal to some other variable that changes (well, of course it changes, that's why it's called a variable). I like to call this variable floor, because it represents the bottom number that get's checked in the loop:
- First select the for loop in the actionScript panel
- Change Init to n=floor
Now we need to increment floor by 1 after a movieClip gets removed. The initial number in the for loop (n) will get larger and larger, and the script will no longer check movieClips that don't even "exist":
- First, select the removeMovieClip script, our new script should run right after this
- Choose Statements > Variables > set variable
- The Variable is called floor
- The Value is floor+1
Of course, we're going to have to declare that floor variable at the start of the script. Remember, if you try to add 1 to a number that doesn't exist yet, Flash will return NaN (Not a Number):
Time to test and make certain everything still works! If you like, you can trace (floor); in the for loop to see what it is doing. You can see in the example below that I added dynamic check boxes on the stage so I can see what's happening with all my variables: