Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First, let me apologize for focusing just on a side aspect of this issue (in my comments to fenomas' answer); though I already made my point and I even agree that object orientation, or at least, a formal class syntax was added to AS1 as an afterthought, called AS2 -- and at some points, it shows --, I neglected answering your actual question.</p> <p>Re-reading this thread, I think it all really boils down to "waiting one frame". That's a workaround I had to use many many times in AS2, so it seems odd I neglected to see that was the real problem here.</p> <p>Anyway, your workaround will probably work, but as you say, it can become a maintenance nightmare really quick. Fenomas' option is another valid workaround. In this case, I wouldn't go that way if I could avoid it, though, but not because of performance; rather, because re arranging a lot of stuff when you already have lay out things could be a lot of work.</p> <p>So, perhaps you could try something really simple like:</p> <pre><code>mc.gotoAndPlay("on"); this.onEnterFrame = function():Void { trace(mc.inst instanceof C); delete this.onEnterFrame; }; </code></pre> <p>And I think it should work. The enterFrame event will be caught by your handler one frame after you register to it. At that point, you now the frame you've moved to is ready, so you just have to clean up the handler and do what you'd normally do just after issuing the gotoAndPlay.</p> <p>I remember I once had to do something very similar (it was not exactly the same scenario, but it boiled down to waiting one frame), so at the time I wrote a very simple class to centralize this code. It was something along these lines:</p> <pre><code>class FrameDelay { function FrameDelay(scope:Object,callback:Function,args:Array) { // get a reference to the current enterFrame handler // (if any), so we can restore it back when we're done var oldEnterFrameHandler:Function = _root.onEnterFrame; _root.onEnterFrame = function():Void { oldEnterFrameHandler(); callback.apply(scope,args); _root.onEnterFrame = oldEnterFrameHandler; } } } </code></pre> <p>And you'd use it like this:</p> <pre><code>mc.gotoAndPlay("on"); new FrameDelay(this,onFrameReady,["a","1"]); function onFrameReady():Void { // arguments (if any), will be available in the arguments array trace(arguments.length); trace(mc.inst instanceof C); trace(this); } </code></pre> <p>You pass a scope, a function and optionally an array of arguments. Most likely, you will not need them, and it could be also possible to just have the class constructor take a callback. But in some circumstances, you could have scope problems in your callback (this is an AS2 issue), so passing the scope explicitly is safer. </p> <p>Also, note that I'm using _root.onEnterFrame. Though I'm making a "backup" of the original handler and restoring it back when I'm done, that doesn't necessarily mean that other parts of the code are that polite (!). So, the handler could be overwritten. If you think that could be a problem, maybe you can dinamically create a movieClip in the root at some depth you know it's not used, and replace _root.onEnterFrame with _root.dummy_mc.onEnterFrame.</p> <p>Anyway, keep in mind that a simple inline onEnterFrame will do the job, so maybe you don't even need using a class for this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload