Mouse Trails

Remove The Right Way

Just when you thought it was too easy to be true... it's time to do things the right way. Sorry to say it, but you're going to have to delete that removeMovieClip line of code from the trail movieClip. We're going to get into some heavy stuff, but it's OK if you don't fully understand it today because we will certainly use it again in the future!

OK, now let's return to the actions on the main timeline. We're going to loop some code so start the onEnterFrame function. Make sure it's independent of the onMouseMove function:

  1. First, select the closing brace for the onMouseMove function... the new script goes outside that brace
  2. Click Statements > User-Defined Functions > function
  3. Type onEnterFrame in the Name box

Now let's try the for loop. This handy script starts with a variable, and loops a set of actions as long as the variable is less than a number:

  1. Find Statements > Conditions/Loops > for
  2. In the Init box type n=0
  3. In the Condition box type n<count
  4. In the Next box type n++

Next we will introduce you to the MovieClip data type.

  1. Click Statements > Variables > var
  2. In the Variables box type mc = _root["trail"+n]
  3. Choose MovieClip from the Type drop-down-list

Now, whenever we want to target one of the many instances of trail that exist on the stage, we can use that variable mc, rather than retyping _root["trail"+n] repeatedly.

Ok, now we want to check and see if mc has reached the end of the timeline. This isn't going to be too bad because Flash has a few built-in functions for us: currentframe and totalframes

If we want to know what frame mc is currently playing, we use: mc._currentframe

If we want to know how many frames are in the mc timeline, we use: mc._totalframes

We know that mc is on the last frame in the timeline when: mc._currentframe==mc._totalframes

So, let's get Flash to check if mc is on the last frame using the if statement:

  1. Click Statements > Conditions/Loops > if
  2. The Condition is mc._currentframe==mc._totalframes

Ok, finally we can remove the movieClip if mc has reached the last frame. This is the same concept as before, just make target mc as the object to remove:

Test it out to make sure it's working! You'll see a bit of problem, which we will fix:

Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player