AS3 - gotoAndStop with immediate action

后端 未结 2 526
青春惊慌失措
青春惊慌失措 2021-01-19 01:55

I\'m moving from AS2 to AS3 and probably as many ppl before found this incompatibility:

I used quite often code like:

gotoAndStop(5);
trace(box); //w         


        
相关标签:
2条回答
  • 2021-01-19 02:46

    no easy way to do that.

    what you need to do is

    • setup a listener for when the frame renders

    • tell it to go to the said frame(5)

    • force the rendering to happen ASAP stage.invalidate

    .

    One of the top reasons to stay with as2. Not saying as2 is better, just better at a few things and this is one of them. My opinion on this is that as3 wasn't really meant to handle timelines very well.

    with as2 you do

    gotoAndStop(5);
    trace(box);
    

    With as3 you need to wait for the timeline to render.

    stage.addEventListener(Event.RENDER, onRenderStage);
    protected function onRenderStage(ev:Event):void {
        trace(this['box']);
    }
    gotoAndStop(5);
    stage.invalidate();
    

    I used to have different assets in different frames of one MovieMlip in my as2 days, but to do that in AS3 is too complicated to enjoy any of the benefits. So while this will work, I'd recommend looking into a different solution altogether. Or stick to as2.

    0 讨论(0)
  • 2021-01-19 02:49

    There is an easy way to solve this, but it is undocumented:

    addFrameScript(1, update);
    gotoAndStop(2);
    
    function update() {
        trace(box); // outputs [object MovieClip]
    }
    

    Please note that the first argument for addFrameScript is the frame number but it's 0-based, i.e. 0 is frame 1, 1 is frame 2, etc... The second argument is the function you would like to call.

    0 讨论(0)
提交回复
热议问题