as3 want to removeEventListener KeyboardEvent to prevent additional text entry and allow only 1 keyboard event

39 Views Asked by At

I'm building a simple animation that I want to advance using any keypress until a certain frame in the timeline where I want to disable any random key press and enable just the Enter key to advance to the next frame.

I'm not great at AS3 and removing EventListener within a timeline doesn't seem to work for me (cuz I'm obviously doing it wrong).

This is my code to advance using any key (except kb Left and Up)

stage.addEventListener(KeyboardEvent.KEY_DOWN, pressAnyKeyAdvanceButLeftGoBackward1);
stage.addEventListener(KeyboardEvent.KEY_UP, resetKeyListener);

function pressAnyKeyAdvanceButLeftGoBackward1(event:KeyboardEvent):void
{
    stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressAnyKeyAdvanceButLeftGoBackward1);
    switch (event.keyCode)
    {
        case Keyboard.LEFT :
            prevFrame();
            break;
                
        case Keyboard.UP :
            gotoAndStop(1);
            break;

        default :
            nextFrame();
            break;
    }
} 

function resetKeyListener(event:KeyboardEvent):void
{
    stage.addEventListener(KeyboardEvent.KEY_DOWN, pressAnyKeyAdvanceButLeftGoBackward1);
} 

I then want to remove (or disable) this and put this in it's place

stage.removeEventListener(KeyboardEvent.KEY_DOWN, pressAnyKeyAdvanceButLeftGoBackward1);

stage.addEventListener(KeyboardEvent.KEY_DOWN, enterkey1);
function enterkey1(event:KeyboardEvent):void {

    if (event.keyCode==13) {
        gotoAndStop(26);
        stage.removeEventListener(KeyboardEvent.KEY_DOWN, enterkey1);
    }
}

This doesn't seem to remove the pressanykey keyboardevent as it still advances with random key presses. Any guidance would be super appreciated.

1

There are 1 best solutions below

0
Will On

Having the resetKeyListener function which re-adds the event listener on keyUp seems slightly unnecessary to me.

If you're worried about the eventlistener being added repeatedly every frame, I would suggest containing your timeline inside a MovieClip and writing the code on the parent so that the code does not get re-executed each time the frame of your timeline animation changes.

I would also suggest that you probably don't need 2 different key handler functions, but could instead just have 1 function which contains logic to determine the resultant behaviour. This way you need not worry about adding and removing event listeners.

For instance, if you have the timeline animation inside a MovieClip called 'timelineMC', your code on the parent might look like this:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);

function keyHandler(event:KeyboardEvent):void
{
    if (timelineMC.currentFrame < 26) 
    {
        switch (event.keyCode)
        {
            case Keyboard.LEFT :
                timelineMC.prevFrame();
            break;
            
            case Keyboard.UP :
                timelineMC.gotoAndStop(1);
            break;
    
            default :
                timelineMC.nextFrame();
            break;
        }
    } else if (event.keyCode == Keyboard.ENTER) {
        timelineMC.nextFrame();
    }
} 

This way if the current frame of the timelineMC is less than 26, the logic of your switch statement will be used. Otherwise, only advance the frame if the keycode is Keyboard.ENTER