Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 - gotoAndStop with immediate action

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); //where box is a movie on 5th frame

What is the easiest way how to do it in AS3.

like image 421
Oldes Avatar asked Feb 25 '11 15:02

Oldes


2 Answers

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.

like image 111
Chris Avatar answered Nov 01 '22 19:11

Chris


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.

like image 42
Daniel Avatar answered Nov 01 '22 18:11

Daniel