Sequencing Events in Flash

While over on the flashkit boards yesterday I was going
through the various posts and lending a hand where I could – as I do most
days – and I came across two people who were asking pretty much the same
question: “How do I sequence events using the mx.transitions.Tween class?”

They were both trying to use the onMotionFinished() event
handler to cause events to fire in a set sequence and wanted to know how to
cause an event to be called at a time other than exactly when the motion from
the Tween was completed.

The problem here is that onMotionFinished() isn’t meant for
hardcore event sequencing… that’s what the timeline is for. The timeline is the
only real reason to buy the full flash suite, and ignoring it, especially for
sequencing is ignorant.

In the end I offered three solutions:

  • Create a series of setInterval() functions that are timed to
    execute your tweens. While not ideally flexible, this method does allow
    manipulation of the sequence in milliseconds, and all from a single screen. You
    must remember to add a clearInterval() to the function that the setInterval()
    calls or else your tween will happen over and over again.
  • The best solution is to place the tween actions on different
    frames and use the timeline to space those frames. This provides an easy to
    adjust, graphical representation of the sequence and maintains the tween class
    benefit of being able to use built-in easing.
  • Another solution, while not ideal for large, complex
    animations, is to take the tween class away, and manually tween the items on
    the timeline.

All of these solutions will work well for static animations
such as those found in intros, scene changes, and button animations, but the
Tween class’s real power is in interactive animation. Interactive animations
are determined at runtime. They use variables that are dependant on the users
actions and are likely to be different every time.

The Tween Class has a cool method called continueTo() which
can give a tween a new destination. If you link that new destination to the
_xmouse or _ymouse properties you’ll quickly see the potential power of the
tween class.

In conclusion, while it is generally best practice to have
all your ActionScript on frame 1 of your Flash movies, it’s ok to use the tween
class (or any ActionScript) in combination with the timeline if you know you
want a series of actions to happen at pre-determined times. That what the
timeline is for.

Tutorial: A Simple script to create a random flicker


To create a method that causes a random flickering effect.

To set this up , create a movieclip on stage and give it an instance name of myCoverUp. The script below, declares a variable on the main timeline so it can be used outside the scope of the function flicker(). Then the function flicker chooses a random number between 0-50, toggles the visibility of myCoverUp, then clears and creates a new interval timer using the random number as a delay.

var nextInterval = 100;
flickerInterval = setInterval(flicker, nextInterval);
function flicker(){
    nextInterval = Math.random() * 50;
    myCoverUp._visible = !myCoverUp._visible;
    flickerInterval = setInterval(flicker, nextInterval);