Flash_and_Flex_03_2009

Animating with ActionScript

Things in ActionScript 3.0. Primarily thanks to the undocumented addFrameScript method of the MovieClip object. As ActionScript 3.0 is an entirely object oriented language, Adobe make use of the addFrameScript method to add your timeline scripts to frames at runtime. This also, however, provides the added bonus of being able to add frame scripts at runtime in your own application classes. Using addFrameScript Let’s look at this now, so you can truly envisage the power of addFrameScript . In our first demonstration, we’re going to manipulate a simple MovieClip timeline using a TimelineManager class, as shown in (Listing 1). We’ll add to the TimelineManager class as we progress through the article, but it won’t become much more complicated than this. As you can see, the class provides a static hook to initialise your MovieClips at runtime using the initClip method. Essentially, by registering your MovieClip’s with the TimelineManager, you can listen for the advent of a frame label notification, without having to have your clips extend any specific class. The TimelineManager will discover each of the frame labels within your MovieClip and assign the passed handler method. Then, when your MovieClip plays and a label is reached, the handler will be fired, notifying you of this event. It is then up to you if you wish to perform any actions for the given label. (Listing 2) provides an example document class using this feature. For the example to work, you will first need to create a MovieClip on the stage of your FLA with a timeline and set labels, see (Figure 1). You can opt to use the same MovieClip name and frame labels as me, or choose your own. However, if you do use your own values, remember to update the document class to match. Should you wish to copy the example, then your MovieClip instance should be labelled _ballAnimation and your frames labelled fShrink , fGrow , fMoveForward and fMoveBackward . If you compile the movie, you should see your animation jumping around and not following the linear animation you created. Employing Events So far so good. You can now detect when a label has been reached in a specific MovieClip and provide custom code that will execute when it does, but this isn’t very flexible. In true ActionScript 3.0 style, it would be better if we could listen for an event, and use a handler for multiple clips. The good news is that this isn’t tough to do, and adding event dispatching support automatically adds provision for passing details of the event to have changed, though,

Listing 2. The document class

package { import flash.display.MovieClip; import TimelineManager; public class Main extends MovieClip { public function Main() { TimelineManager.initClip( _ballAnimation, handleFrameLabel ); }

private function handleFrameLabel() : void { _flText.text = _ballAnimation.currentLabel; switch ( _ballAnimation.currentLabel ) { case "fMoveForward": _ballAnimation.gotoAndPlay( "fShrink" ); break ; case "fMoveBackward": _ballAnimation.gotoAndPlay( "fGrow" ); break ; } } } }

Listing 3. TimelineManager class with event support

package { import flash.display.MovieClip; import flash.display.FrameLabel; import FrameEvent; public class TimelineManager extends MovieClip { public function TimelineManager() { super (); initClip( this , handleFrameLabel ); } private function handleFrameLabel() : void { var f : FrameLabel = new FrameLabel( currentLabel, currentFrame ); dispatchEvent( new FrameEvent( FrameEvent.LABEL, new FrameLabel( currentLabel, currentFrame ) ) ); } public static function initClip( mc : MovieClip, handler : Function ) : void {

var labels : Array = mc.currentLabels; for each ( var i : FrameLabel in labels ) mc.addFrameScript( i.frame – 1, handler ); } } }

Listing 4. The FrameEvent class

package { import flash.events.Event; import flash.display.FrameLabel; public class FrameEvent extends Event { public static var LABEL : String = "frame label event"; public var frameLabel : FrameLabel; public function FrameEvent( str : String, label : FrameLabel ) { super ( str ); frameLabel = label; } } }

03/2009 (5)

21

Made with FlippingBook HTML5