Flash_and_Flex_03_2009

ActionScript Development

your handler, so you don’t have to do too much work to get this going. To make this clean, we can add the necessary code to the TimelineManager class, so little overhead will be added to your overall code. The drawback to this, however, is that you’re going to have to have your MovieClips inherit from the TimelineManager class, either directly, or through a custom class. Also, as we’re now using events, we’ll need to incorporate a new FrameEvent class, which will be passed to the event handler method. (Listing 3) shows the updated Timeline- Manager class, complete with event dispatching, while, see (Listing 4) details the new FrameEvent class. To use this new feature, you’ll need to import the FrameEvent class into your document class, and update the event handler to accept the event. As the MovieClip is now inheriting the TimelineManager class, you will no longer need to pass the clip to initClip, package { import flash.display.MovieClip; import TimelineManager; import FrameEvent; public class Main2 extends MovieClip { public function Main2() { Listing 5. Document class employing events

but you will need to register your listener for the event in the usual way. (Listing 5) shows an example document class using the new event. As you can see, other than when the event is registered with the handler, there’s no longer any need to specifically use the MovieClip instance name. Animation Stacks We’re off to a good start. For the most part, this code should provide a way to reduce much of the refactoring commonly required in timeline based FLA’s prior to coding, but there’s still more we can do. Assuming you, the developer, are given an FLA with multiple MovieClips, each with their own timelines, you would probably benefit from a way to stack the execution of those animations so that they play synchronously. Such is common when developing Flash based movies or games, where dialogue with corresponding actions is important. (Listing 6) provides new

code for our TimelineManager class to do just that. There are two new methods in our TimelineManager, now, as well as two new private class variables. The method addAnimationToStack provides the means to add animation parameters to the stack contained in the _animStack array. Ordinarily, a new class would be drawn up to handle such data, but in the interests of trimming the fat from this article, anonymous objects would do fine. The animation parameters include the MovieClip instance to be animated, the starting label name and the ending label name. As the MovieClip is positioned to be animated, it will jump to the starting label, and animate until the end label is found. Now, when the current end label for the executing animation is detected, it will check if there are more animations waiting on the stack, and if there are, will fire the

super (); initClip( this , handleFrameLabel ); } private function handleFrameLabel() : void { var f : FrameLabel = new FrameLabel( currentLabel, currentFrame ); dispatchEvent( new FrameEvent( FrameEvent.LABEL, new FrameLabel( currentLabel, currentFrame ) ) ); if ( _animStack && _animStack.length > 0 ) animateWithStack( this ); } 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 ); } public static function addAnimationToStack( clip :

_ballAnimation.addEventListener( FrameEvent.LABEL, handleFrameLabel ); } private function handleFrameLabel( event : FrameEvent ) : void { switch ( event.target.currentLabel ) { case "fMoveForward": event.target.gotoAndPlay( "fShrink" ); break ; case "fMoveBackward": event.target.gotoAndPlay( "fGrow" ); break ; } } } }

MovieClip, startLabel : String, endLabel : String ) : void {

if ( !_animStack ) _animStack = new Array(); _animStack.push( { clip : clip,startLabel : } private static function animateWithStack( clip : TimelineManager ) : void { var curAnim = _animStack[ _pointer ]; if ( clip.currentLabel == curAnim.endLabel ) { clip.stop(); _pointer++; if ( _pointer >= _animStack.length ) _pointer = 0; curAnim = _animStack[ _pointer ]; curAnim.clip.gotoAndPlay( curAnim.startLabel ); } } } }

startLabel,endLabel : endLabel } );

Listing 6. TimelineManager class with animation stack support

package { import flash.display.MovieClip; import flash.display.FrameLabel; import FrameEvent; public class TimelineManager extends MovieClip {

private static var _animStack : Array; private static var _pointer : int = 0; public function TimelineManager() {

03/2009 (5)

22

Made with FlippingBook HTML5