A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Keyboard Events for Right and Left keys

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    29

    Keyboard Events for Right and Left keys

    Here is my code:

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler71);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler71);
    stage.addEventListener(KeyboardEvent.Key.RIGHT, keyRightHandler71);
    stage.addEventListener(KeyboardEvent.Key.ESCAPE, keyEscapeHandler71);
    stage.addEventListener(KeyboardEvent.Key.ENTER, keyEnterHandler71);

    function keyDownHandler71(event:KeyboardEvent):void {
    gotoAndPlay("edgeofdarkness");
    }

    function keyUpHandler71(event:KeyboardEvent):void {
    gotoAndPlay("home1");
    }

    function keyRightHandler71(event:KeyboardEvent):void {
    gotoAndPlay("avatar");
    }

    function keyEscapeHandler71(event:KeyboardEvent):void {
    gotoAndPlay("end");
    }

    function keyEnterHandler71(event:KeyboardEvent):void {
    gotoAndPlay("loadingitscomplicated");
    }
    The code for KEY_UP and KEY_DOWN seems to work fine. But, the rest of it returns the error "1119: Access of possibly undefined property Key through a reference with static type Class." What the hell?

    How do you call a function for Right, Left, Enter, and Escape Keys?

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    Notice that KeyboardEvent.KEY_DOWN and KeyboardEvent.KEY_UP are two events. One for when you press a key and another one for when you release it.
    It has nothing to do with the up and down arrow.

  3. #3
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    see this example:
    Actionscript Code:
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);


    function keyDownHandler(e:KeyboardEvent):void {
        switch(e.keyCode)
        {
            case 38:
            trace("up");
            break;
           
            case 40:
            trace("down");
            break;
           
            case 39:
            trace("right");
            break;
           
            case 27:
            trace("escape");
            break;
           
            case 13:
            trace("enter");
            break;
           
        }
    }

    function keyUpHandler(e:KeyboardEvent):void {
    }
    You need to disable shortcut keys when testing in the test player.
    Control->Disable Shortcut Keys

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    29
    Thanks for that. I was confused. Now, how do I get it to gotoandplay a particular frame when a particular key is pressed?

  5. #5
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Wherever it says trace("XXX"); you replace with the code you want to be ran every time the key is pressed.
    Also, note that the event listener is called repetitively when the key is held down.

  6. #6
    Junior Member
    Join Date
    Jul 2010
    Posts
    29
    I have tried the following code:

    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

    function keyDownHandler(event:KeyboardEvent):void {
    switch(e.keyCode)
    {
    case 40:
    //Down Key
    gotoandplay (4);
    break;

    case 39:
    //Right Key
    gotoandplay (2);
    break;
    }
    }
    It is returned with the error "1180: Call to a possibly undefined method gotoandplay."

  7. #7
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    You didn't capitalize A and P.
    gotoAndPlay is the correct function.

  8. #8
    Junior Member
    Join Date
    Jul 2010
    Posts
    29
    I am so stupid. Thank you again. But, I am close to giving up. Even after I corrected all of this, it still won't function properly.

    Just to make sure I have understood keyboard events, I created a very small flash file with 4 colored boxes. The arrow keys on the keyboard should move the red box around the screen.

    Here is a download link to the flash file.

    The red box does not cooperate properly.

  9. #9
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    Use gotoAndStop() instead of gotoAndPlay().
    Remove the listeners after you use them.
    Attached Files Attached Files

Tags for this Thread

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