AS3: How to jump to another scene (or frame sequence) and return back to frame having jumped from

I’m rather a newbie on the AS3 bit so my question might not be too hard to answer for someone. Hope I can explain it clearly enough. Apologies for the extent of it :wink:

Right now I’m building a simulation of a sports match, played in 2 linear halves, divided in 2 separate scenes. Goals that have been scored in-match appear as a button on the stage as scoring occurs. The button works as a reference to replay the goal. Goals scored in let’s say the first half (scene 1) should be accessible to replay whilst in the second half (scene 2). I’ve created extra scenes per goal that serve as individual replays to refer to.

What I would like to have the user be able to, is to click that replay button at any chosen time, view the replay, and then return to the exact frame it originally jumped from, and play on from there when preferred.

The reason why I ‘cannot’ use movieClips and addChild methods, is that I want the final SWF to keep its transparent background, as I will embed it in html later on.

If (and how?) I use the addChild method, you see 2 partially transparent movieClips on top of each other, running at the same time.

If I’d find out how to replace the one with the other, this could be suited method.

Another would be using currentFrame currentLabel and/or prevFrame methods I guess but I wouldn’t know how to (yet).

Who could help me out with this, or point me into some directions? I’m kind of stuck on this one.

I have the project .fla file to give you a clearer insight on things.

Thanks a million in advance!

Found the answer:

Let’s assume you have a replay button with the instance name of replayBtn. Here is one way to set this up:

On the first frame of your timeline, create the following vars and functions:

//define a variable to hold the last replay frame
var lastReplayFrame:int = 0;

//define a variable to hold the frame to go back to once a replay is done
var replayReturnFrame:int = 0;

//create a function to call after a replay is done
function replayReturn(){
//if we’ve set a frame to jump back to
if(replayReturnFrame > 0){
//go there and resume playing
this.gotoAndPlay(replayReturnFrame);

    //reset the return frame so we don't go back there the next time if the button isn't hit
    replayReturnFrame = 0;
}

}

//create the click handler for the replay button:
function replayClick(e:Event):void {
//check if there is a replay available
if(lastReplayFrame > 0){
//store the current frame before jumping back
replayReturnFrame = this.currentFrame;

    gotoAndPlay(lastReplayFrame);
}

}

//add the click event listener to the replay button instance
replayBtn.addEventListener(MouseEvent.CLICK, replayClick);
Now, in your timeline, whenever a goal starts (eg the first frame of where a replay should start), put the following code on a keyframe:

lastReplayFrame = this.currentFrame;
Then, on the last frame of a replay, put this code on a keyframe:

replayReturn();