package { import flash.display.MovieClip; import flash.display.Stage; import flash.events.Event; /** * Utils author:Bruce Branscom BruceBranscom@yahoo.com * * This class is meant to help get around the AS3 bug with child MovieClips becoming null * after calling gotoAndPlay() or gotoAndStop() * * Usage: * * Utils.setStage( stage ); //this only needs to be called once every application * * Utils.gotoAndPlay( myMC, "frameLabel", completeFunction ); * * function completeFunction():void * { * you can access myMC's children here * } * * * You can also pass parameters to the complete function: * * Utils.gotoAndStop( myMC, 3, completeFunction, [ param1 , param2 ] ); * * function completeFunction( param1:*, param2:* ):void * { * you can access myMC's children here and the passed in params * } * */ public class Utils { private static var _stage:Stage; public static function setStage( stage:Stage ):void { _stage = stage; } public static function gotoAndPlay( mc:MovieClip, frame:*, completeFunction:Function, params:Array = null ):void { mc.addEventListener( Event.RENDER, frameComplete ); mc.gotoAndPlay( frame ); mc.params = params; mc.callFunction = completeFunction; //forces the render function to fire _stage.invalidate(); } public static function gotoAndStop( mc:MovieClip, frame:*, completeFunction:Function, params:Array = null ):void { mc.addEventListener( Event.RENDER, frameComplete ); mc.gotoAndStop( frame ); mc.params = params; mc.callFunction = completeFunction; //forces the render function to fire _stage.invalidate(); } private static function frameComplete( e:Event ):void { e.target.removeEventListener( Event.RENDER, frameComplete ); if ( e.target.params ) { e.target.callFunction.apply( null, e.target.params ); e.target.params = null; } else e.target.callFunction.call(); e.target.callFunction = null; } } }