0
votes

Just to give you a preface, I started messing with event handlers to fix a jerky animation transition when jumping from one set of looped frames to another all within the same time line.

Ok, let's say I have an animation of a horse running consisting of 5 frames. Now let's say I have three different movie clip loops of these frames at different speeds (different fps really): walk, trot, and gallop.

Now let's say I've created a button that lets the viewer progress through each set of loops and then another to let them replay back from the beginning. My initial problem arose when jumping from say the first loop (1-5) to the next loop (6-10) because essentially at any point when hitting that forward button I'm stopping the loop and starting it again from the beginning. Hence the hiccup in the animation.

To combat this I found an example online where you can use an event listener to check what frame you're at when the button is pushed then lets it complete the loop and then move on, thus negating any weird flinch.

Here's that event code:

forward1.addEventListener(MouseEvent.CLICK, trot); 

function trot(event: MouseEvent): void { 

addEventListener(Event.ENTER_FRAME, enterFrame); 
function enterFrame(e:Event):void { 
    if (currentFrame == 5) { 
        gotoAndPlay(6); } 

    } 
}

Meanwhile the next one would look like this:

forward2.addEventListener(MouseEvent.CLICK, gallop); 

function gallop(event: MouseEvent): void { 

addEventListener(Event.ENTER_FRAME, enterFrame); 
function enterFrame(e:Event):void { 
    if (currentFrame == 10) { 
        gotoAndPlay(11); } 

    } 
}

I don't know if it's important btw, but I'm creating my loops on the very last frame of each group and sending it back to the first frame (of that group).

So, I've got on of those listeners on buttons at each set of loops and it plays through great...the first time. Here's where my current issue comes in. When getting to the very end of the movie I've got this:

playagain.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_1);

function fl_ClickToGoToAndPlayFromFrame_1(event:MouseEvent):void
{
    gotoAndPlay(1);
}

Which basically just takes the playhead back to the first frame on click. BUT, now instead of waiting for the user to click to forward the animation to the second loop set, the movie just auto plays through the entire thing.

My assumption is that the event listeners from the first play-through are still there and running. So, essentially Flash is seeing "Hey when you get to frame X go on to frame Y" "When you get to frame Y go to frame Z" and so on.

How do I remove those listeners after they've fired? Can I remove them all at once before the replay?

Or, is there a better way to check where the playhead is when jumping between two sets of loops, finishing whatever loop it's on before going where I tell it to?

1

1 Answers

1
votes

You are correct. You just need to remove the event listeners. I would avoid nesting your functions though. Try this:

forward1.addEventListener(MouseEvent.CLICK, trot); 

function trot(event: MouseEvent): void {
    addEventListener(Event.ENTER_FRAME, enterFrame); 
}

function enterFrame(e:Event):void { 
    if (currentFrame == 5) { 
        removeEventListener(Event.ENTER_FRAME, enterFrame); 
        gotoAndPlay(6);
    }
}