Skip navigation
theBigBoppa
Currently Being Moderated

Can't get keyboard command to work right

Jun 13, 2012 10:44 AM

Hello,

I'm trying to get 2 different sets of keys to move a character in a game. "WASD" and the arrow keys. The arrow keys work fine, but I can't get the character to move to the left witht the "A" key after I press the "D" key. Can anyone tell me what to do? I think it might have something to do with the focus? But I don't know anything about that......Here's my code:

public function Player(model:GameModel, stage:Object, playerX:Number, playerY:Number) {

                              // constructor code

                              _gameModel = model;

                              _stage = stage;

                              xPos = playerX;

                              yPos = playerY;

 

                              _stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler, false, 0, true);

                              _stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler, false, 0, true);

 

private function keyDownHandler(event:KeyboardEvent):void

                    {

                              if (_gameModel.gameMode != "play") return;

                              processKeyDown(event);

                    }

                    private function keyUpHandler(event:KeyboardEvent):void

                    {

                              if (_gameModel.gameMode != "play") return;

                              processKeyUp(event);

                    }

 

                    internal function processKeyDown(event:KeyboardEvent):void

                    {

 

 

 

                      if(event.keyCode == Keyboard.A)||

 

                               (event.keyCode == Keyboard.LEFT))

 

                      {

 

                                walk = true;

                        acceleration_X = -aSpeed;

 

                      }

                      if((event.keyCode == Keyboard.D)||

                               (event.keyCode == Keyboard.RIGHT))

                      {

                                walk = true;

                        acceleration_X = aSpeed;

 

                      }

                      if((event.keyCode == Keyboard.UP)||

                               (event.keyCode == Keyboard.W))

                      {

 

                       jump=true;

                      }

                      if((event.keyCode == Keyboard.DOWN)||

                               (event.keyCode == Keyboard.S))

                      {

                                //crouch = true;

 

                      }

 

                     

 

 

                    }

 

                    internal function processKeyUp(event:KeyboardEvent):void

                      {

                        if (event.keyCode == Keyboard.LEFT

                        ||event.keyCode == Keyboard.RIGHT||

                     event.keyCode == Keyboard.A

                        ||event.keyCode == Keyboard.D)

                     {

 

 

 

 

                        acceleration_X = 0;

                               walk = false;

                      }

                      if((event.keyCode == Keyboard.UP)||

                               (event.keyCode == Keyboard.W))

                      {

 

                         jump = false;

                      }

                       if((event.keyCode == Keyboard.DOWN)||

                               (event.keyCode == Keyboard.S))

                      {

                         acceleration_Y = 0;

                         //crouch = false;

                      }

 
Replies
  • kglad
    72,209 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 13, 2012 12:20 PM   in reply to theBigBoppa

    the first error i see is in this line:

     

    if(event.keyCode == Keyboard.A)||

     

                                   (event.keyCode == Keyboard.LEFT))

     

                          {

     

    actually, i'm not sure whether that is supposed to be one line of code or not.  but no matter, it's still not possible for flash to parse that code because there are mismatched parantheses.

     
    |
    Mark as:
  • kglad
    72,209 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 13, 2012 3:26 PM   in reply to theBigBoppa

    copy and paste your formatted code (accurately).

     
    |
    Mark as:
  • kglad
    72,209 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 14, 2012 6:13 AM   in reply to theBigBoppa

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points