When I migrated to AS3 last year, there’s always one thing bugging me when using “gotoAndStop” method to jump to certain frames…
If you jump to a label called “transIn” and there is a movie clip called “close_btn” and assign the code to “close_btn” will cause the error “TypeError: Error #1009: Cannot access a property or method of a null object reference” since it cannot find the movie clip (AS2 does not have this bug), e.g:-
1 2 | myClip.gotoAndStop("transIn"); myClip.close_btn.addEventListener(MouseEvent.CLICK, onCloseBtnUp); |
I have created a movie clip buffering class to solve this issue that has been implemented to all of my projects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package { // Import class import flash.display.*; import flash.utils.*; import flash.events.*; // McBuffer public class McBuffer { // Class ID public static const CLASS_ID:String = "McBuffer"; // Properties private var __timer:Timer; private var __target:MovieClip; private var __name:String; private var __func:Function; // Constructor function McBuffer(target:MovieClip=null, name:String=null, func:Function=null) { __target = target; __name = name; __func = func; buffer(); } ///////////////////////////////////////////////////////////////////////////////// // private methods ///////////////////////////////////////////////////////////////////////////////// // Buffering private function buffer():void { __timer = new Timer(100, 0); __timer.addEventListener("timer", onCheckMC); __timer.start(); } // Check MovieClip private function onCheckMC(event:TimerEvent):void { if(__target.getChildByName(__name)){ __timer.removeEventListener("timer", onCheckMC); __func(); __timer = null; __target = null; __name = null; __func = null; } } } } |
So if we have the same label and movie clip, we can use this:
1 2 3 4 5 6 7 | myClip.gotoAndStop("transIn"); // Set clip target and the instance name of the mc new McBuffer(myClip, "close_btn", setClip); // Function to call function setClip():void { myClip.close_btn.addEventListener(MouseEvent.CLICK, onCloseBtnUp); } |
Although it works perfect on all of my projects, I have always wondering why didn’t Adobe solve this annoying bug before?
…and here comes the “addFrameScript” undocumented method for movie clip which I have found on the web recently (Actually I have came across to this method last summer but I wasn’t aware that it will solve this problem).
Basically “addFrameScript” takes 2 parameters: frame number (beware it is zero index so 5 will be frame 4 instead) and the method to call/assign on this frame:
1 2 3 4 5 6 7 8 9 | myClip.gotoAndStop("transIn"); // Add a frame script to "transIn" by checking its current frame myClip.addFrameScript(myClip.currentFrame-1, setClip); // Function to call function setClip():void { // Remove frame function myClip.addFrameScript(myClip.currentFrame-1, null); myClip.close_btn.addEventListener(MouseEvent.CLICK, onCloseBtnUp); } |
It looks neater by using built-in method so I think I will replace my method with this instead:)
No user commented in " AS3 addFrameScript method "
Follow-up comment rss or Leave a TrackbackLeave A Reply