Responsible Animation Class
snipped by lolablissie
This is a general pattern I use for a class that performs and animates responsibly. The trick it to use this pattern for an object and all of its children; the general pattern is 1)Instantiate 2)Call loadAndStart() and when done with the object 3)stopAndUnload()
package { import flash.events.TimerEvent; import flash.utils.Timer; //import flash.display.Sprite; /** * ... * @author Nina Pavlich */ public class ResponsibleClass extends Sprite { private var DEBUG:Boolean = false; private var startflag:Boolean = false; private var stopflag:Boolean = false; private var className:String="ResponsibleClass"; //public static const MODE_VARIABLE:int =0; //variables used to define particular animation behavior private var animT:Timer; public function ResponsibleClass(debug:Boolean=false) { traceln("Being Instantiated"); DEBUG=debug; } public function load():void { traceln("Loading"); //set vairables addVisualElements(); //children.load() loadDone(); // or dispatch when some kind of data loading is done } public function start():void { traceln("Starting"); configureListeners(); configureIntro(); //children.start(); } public function stop():void { traceln("Stopping"); //children.stop(); configureOutro(); } public function unload():void { traceln("Unloading"); removeListeners(); removeVisualElements(); //children.unload(); nullify(); } public function loadAndStart():void{ traceln("Load And Start"); startflag=true; load(); } public function stopAndUnload():void{ traceln("Load and Stop"); stopflag=true; stop(); } private function nullify():void { traceln("Nullify"); animT=null; //children=null; } private function addVisualElements():void { traceln("Adding Visual Elements"); } private function removeVisualElements():void { traceln("Removing Visual Elements"); } private function configureListeners():void { traceln("Configuring Listeners"); animT = new Timer(50); animT.addEventListener(TimerEvent.TIMER, animate, false, 0, true); } private function removeListeners():void { traceln("Removing Listeners"); animT.removeEventListener(TimerEvent.TIMER, animate); } private function configureIntro():void { traceln("Configuring Intro"); if(!animT.running){animT.start()} } private function configureBody():void { traceln("Configuring Body"); if(!animT.running){animT.start()} } private function configureOutro():void { traceln("Configuring Outro"); if(!animT.running){animT.start()} } public function loadDone():void { traceln("Done Loading"); if(startflag){start()} } public function introDone():void { traceln("Intro Done"); if(animT.running){animT.stop()} configureBody(); } public function outroDone():void { traceln("Outro Done"); if(animT.running){animT.stop()} if(stopflag){unload();} } public function animate(ev:TimerEvent):void { } public function traceln(inStr:String):void { if(DEBUG){trace(className+": "+inStr)}; } } }




