A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Press a key from the keyboard and move to next frame

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Posts
    107

    Press a key from the keyboard and move to next frame

    Hi everyone,

    How do you press a key (like "L") and go to the next frame?

    Which event must I pick?

    I assume it's something like: stage.addEventListener(Keyboard., but after that I don't know what to do.

    This is for AS 3.0 by the way.

    Thanks.

  2. #2
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    El bump.

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Bump.

  4. #4
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    PHP Code:
    stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyboardDown,false,0,true);

    function 
    onKeyboardDown(e:KeyboardEvent):void{
      
    trace(e.keyCode);
      switch (
    e.keyCode){
        case 
    76:
          
    nextFrame();
          break;
        default:
          
    //do nothing
          
    return;
      }

    Follow me on Twitter: http://twitter.com/jasondefra

  5. #5
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Thanks.

    But it also affects all frames after the one where the code is. I just wanted to happen in one particular frame.

    I'm placing the code on the timeline, and each of of the next frames only have:
    PHP Code:
    stop(); 
    Must I remove the eventListener?

  6. #6
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    yeah, if you only want that event to happen on the one frame, add this to the switch statement under "case 76:"

    PHP Code:
    stage.removeEventListener(KeyboardEvent.KEY_DOWN,onKeyboardDown); 
    And yeah, keep the stop actions at the top/bottom of your script (doesn't matter where it is as long as it's there).
    Follow me on Twitter: http://twitter.com/jasondefra

  7. #7
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Yah, that was what I did. But isn't there a way for the code to affect on that particular frame only?

    It's not very code effective pasting the same line of code on all the other frames.

    Thanks.

  8. #8
    Bald By Choice jasondefra's Avatar
    Join Date
    Mar 2008
    Location
    Wisconsin, USA
    Posts
    205
    Not sure what you mean. It is only affecting that one frame since you kill the event listener after you're off that frame. You want to use that function on other frames too? Then just re-add the event listener on those frames (you won't need to re-define the method, so no copypasta and it should still work fine).
    Follow me on Twitter: http://twitter.com/jasondefra

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center