Exerpts from
"Time-Based & Bézier Curve Animation"

a presentation at the 1997 Macromedia International User
Conference by Darrel Plant, Moshofsky/Plant

This Demo requires Shockwave for Director 6.

Time-based animation is an ideal technique to maximize animation compatibility across multiple processors and playback environments. By linking the speed at which a sprite moves to elapsed time rather than aspecific frame rate, the maximum processing power of a processor can be harnessed, without having to choke it back to accomodate slower machines. It has the added advantages of being able to provide smooth animation without locking out mouse and key events.

A typical animation technique is to use a repeat loop to move a sprite from point A to point B. In traditional techniques, a series of discrete steps is taken by the sprite:

  on exitFrame
    repeat with i = 1 to 10
      set the locH of sprite 4 = the locH of sprite 4 + 10
      set the locV of sprite 4 = the locV of sprite 4 + 10
    end repeat
  end exitFrame

In this type of animation, the sprite will move 100 pixels in each direction, with each step taken as fast as the processor can manage to affect the loc property and update the stage. It also has the effect of locking out events. Because different processors will executethe repeat loops faster than others, it's also unpredictable.

Time-based animation, on the other hand is eminently predictable. Typically, an animation needs to take some amount of time, it's simply necessary to define what that time (called a period is).

For simplicity's sake, this example uses some global variables, but more sophisticated approaches would use properties of either behaviors or objects:

  (in Movie Script)
  global gMoveSprite, gMoveSpriteStartMove, gMoveSpritePeriod
  global gMoveSpriteOrigin, gMoveSpriteVector

  on startMovie
    set gMoveSprite = 4            -- arbitrary sprite channel
    set gMoveSpritePeriod = 60     -- in ticks
    set gMoveSpriteOrigin = the loc of sprite 4
    set gMoveSpriteVector = point (100,100)
  end movesprite

  (in frame script before movement)
  global gMoveSpriteStartMove

  on exitFrame
    set gMoveSpriteStartMove = the ticks
  end exitFrame

  (in frame script of movement)
  global gMoveSprite, gMoveSpriteStartMove, gMoveSpritePeriod
  global gMoveSpriteOrigin, gMoveSpriteVector

  on exitFrame
    set nowTime = the ticks
    if nowTime >= gMoveSpriteStartMove + gMoveSpritePeriod then
      set the loc of sprite gMoveSprite = gMoveSpriteOrigin + gMoveSpriteVector
      set elapsedTime = the ticks - gMoveSpriteStartMove
      set elapsedVector = gMoveSpriteVector * elapsedTime / gMoveSpritePeriod
      set the loc of sprite gMoveSprite = gMoveSpriteOrigin + elapsedVector
    end if
    go the frame
  end exitFrame 

This method ties the movement of the sprite to the percentage of the movement period that has elapsed since the sprite began moving. It allows for events, it can handle multiple sprites, and it's ideal for situations where the playback machine may have other tasks operating in the background.

To paraphrase the code, the original position (gMoveSpriteOrigin) of the sprite is stored, along with the amount of time alloted to the animation (gMoveSpritePeriod), and the movement vector (gMoveSpriteVector)--stored as a point value in this case. In the frame before the sprite begins to move, the current time is stored as gMoveSpriteStartMove.

In the frame where the animation actually occurs, the amount of time that has elapsed since the sprite first moved is determined. If the full period has elapsed, the sprite is set to its final destination and the playback head moves on. If the period has not elapsed, the movement vector is multiplied by the proportion of elapsed time divided by the total period. That modified vector is then added to the original position of the sprite, and the playback head repeat the frame until the full period elapses.

In the Shockwave movie above, the sprites move to random positions on the screen on each mouse click. Each sprite's period is determined by the values for "Period" and "Variability" (spritePeriod = Period + random (Variability)). By modifying the settings for both values, you can get a sense of just how this type of animation can work. Clicking while the sprites are moving sets their vectors to new locations without having to complete the current animation sequence.

Visit DirectOregon!

© 1997 by Darrel Plant, Moshofsky/Plant. Permission is granted to link to these pages.