Search code examples
actionscript-3

Having trouble with my Flash Comic


I'm making a webcomic in Adobe Animate right now, and the way a viewer can turn the "page" is by clicking on the screen.

First, I put a code on one frame shown as follows:

import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

stop();
stage.addEventListener(MouseEvent.CLICK,forward2);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward);


function forward2 (event:MouseEvent) :void {
gotoAndStop(currentFrame+1);
}

function forward (e:KeyboardEvent): void {
if (e.keyCode == Keyboard.RIGHT)
gotoAndStop(currentFrame+1);
}

Then, if I wanted a scene to play out without the viewer having to click on the screen, I wrote another code:

import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

stop();
stage.addEventListener(MouseEvent.CLICK,forward4);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward3);


function forward4 (event:MouseEvent) :void {
gotoAndPlay(currentFrame+1);
}

function forward3 (e:KeyboardEvent): void {
if (e.keyCode == Keyboard.RIGHT)
gotoAndPlay(currentFrame+1);
}

So because the function was already defined on another frame, I posted this code on the frame after the animation:

import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;

stop();
stage.addEventListener(MouseEvent.CLICK,forward2);
stage.addEventListener (KeyboardEvent.KEY_DOWN,forward);

I tried to make sure that the script was constant throughout the frames I wanted it to be used for by inserting a blank frame for each frame I wanted it to be active on. However, when I tested it, as soon as I clicked on the frame I put the code on (The one without the function defined), it just sent me back to the beginning.

What am I doing wrong?


Solution

  • When you add a listener to any object it will continue to exist until it is explicitly removed.

    Before going to the next frame you should run:

    stage.removeEventListener(MouseEvent.CLICK, forward2);
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, forward);
    

    This will remove the event listeners and prevent forward2 and forward from running when you click or press a key on some other frame.