O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Generalized Animator

In many examples presented so far in this chapter, the code for creating a visual object and the code for animating that object has been combined in the same class. In real-world applications, it's wiser and more typical to externalize animation code and reuse it across multiple classes.

The simple Animator class shown in Example 24-7 demonstrates one possible way to abstract animation features away from objects being animated. Each Animator instance can move a specified DisplayObject to a given position within a given amount of time. Note that Example 24-7 focuses on the class structure used to externalize animation code. The Animator class does not, therefore, provide sophisticated animation capabilities. For more advanced animation features, consider using the Flex framework's mx.effects.Tween class or Flash CS3's fl.transitions.Tween and fl.motion.Animator classes.

The Animator class listing follows. Comments will guide you through the code.

Example 24-7. A generalized animation utility class

package { import flash.display.*; import flash.events.*; import flash.utils.*; // A very simple animation class that demonstrates how to separate // animation duties from an object being animated public class Animator extends EventDispatcher { // Refers to the object being animated private var target:DisplayObject; // Each time an animation starts we record its start time in startTime private var startTime:Number; // The duration of an animation, in milliseconds private ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required