Question

I'm new to AS3 and although I was looking for a solution to my problem truly long time, I was not successful. I have an animated presentation and I just want to make navigation through that by arrows. Everything is on the main timeline. In the first frame I made this code

var zpet: Number;
if (currentFrame == 1) {
    zpet = 1;
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, posun);

function posun(event: KeyboardEvent): void {
    if (event.keyCode == 37) {
        addEventListener(Event.ENTER_FRAME, playReverse);

        function playReverse(event: Event): void {
            if (currentFrame == zpet) {
                stopPlayReverse();
            } else {
                prevFrame();
            }
        }

        function stopPlayReverse(): void {
            if (hasEventListener(Event.ENTER_FRAME)) {
                removeEventListener(Event.ENTER_FRAME, playReverse);
            }
        }
    } else if (event.keyCode == 39) {
        if (currentFrame < totalFrames - 1) {
            play();
        } else {
            stop();
        }
    }
}
stop();

Moving forward works perfect as I put stop(); in every keyframe of the presentation. The problem is how to go back just to the previous keyframe (and I also want to go back in reverse playing). I thought it would be quite easy if I made a variable (called "zpet") and set it the specific number of frame where to go back in each keyframe. But it doesn't work, all the time it's going back to frame 1. For example I put in the frame 26 the code zpet = 13; that should say when playing back from the frame 26 stop at the frame 13. Any ideas how to solve this? I would be really grateful for that..

Was it helpful?

Solution

You can label each keyframe of your animation anything you want directly from the timeline and then something like this :

function playReverse(event: Event): void
{
    prevFrame();// You can also use gotoAndStop(currentFrame - 1)
    if(currentFrameLabel != null)
        stopPlayReverse();
}

Looks cleaner imo, plus you can use labels value later in case you make a scene selection menu.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top