• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Can't get keyboard command to work right

New Here ,
Jun 13, 2012 Jun 13, 2012

Copy link to clipboard

Copied

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;

                      }

TOPICS
ActionScript

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2012 Jun 13, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2012 Jun 13, 2012

Copy link to clipboard

Copied

sorry I copied it wrong, it's suppose to be:

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

                      {

then it runs but I get the problem above

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 13, 2012 Jun 13, 2012

Copy link to clipboard

Copied

copy and paste your formatted code (accurately).

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2012 Jun 13, 2012

Copy link to clipboard

Copied

Here is the top half of the class, not including the variable declarations at the beginning:

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);

                              _stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveListener, false, 0, true);

                              _stage.addEventListener(MouseEvent.MOUSE_DOWN, shootListener, false, 0, true);

                              _stage.addEventListener(MouseEvent.MOUSE_UP,on_mouse_up, false, 0, true);

 

 

                              this._mouseX = mouseX;

                              this._mouseY = mouseY;

                              //playerShot = new PlayerShot(_xPos,_yPos, _mouseX, _mouseY, _gameModel);

 

                              _spriteGridClass1.initTileSheetData();

                              playerHeadLegsRt = new BlitSprite(_tileSheet, _spriteGridClass1.headLegsRightFrames, 0);

 

                              _spriteGridClass1.initTileSheetData();

                              playerHeadLegsLft = new BlitSprite(_tileSheet, _spriteGridClass1.headLegsLeftFrames, 0);

 

                              _spriteGridClass1.initTileSheetData();

                              playerHoldRot = new BlitSprite(_tileSheet, _spriteGridClass1.holdRotFrames, 0);

 

                              _spriteGridClass1.initTileSheetData();

                              getPistol = new BlitSprite(_tileSheet, _spriteGridClass1.getPistolFrames, 0);

 

                              _spriteGridClass1.initTileSheetData();

                              getMG = new BlitSprite(_tileSheet, _spriteGridClass1.getMGFrames, 0);

 

                              _spriteGridClass2.initTileSheetData();

                              getShot = new BlitSprite(_tileSheet2, _spriteGridClass2.getShotFrames, 0);

 

                              _spriteGridClass2.initTileSheetData();

                              getFT = new BlitSprite(_tileSheet2, _spriteGridClass2.getFtFrames, 0);

 

                              //***weapons

                              _spriteGridClass2.initTileSheetData();

                              pistolRot = new BlitSprite(_tileSheet2, _spriteGridClass2.pistolRotFrames, 0);

 

                              _spriteGridClass2.initTileSheetData();

                              FTRot = new BlitSprite(_tileSheet2, _spriteGridClass2.ftRotFrames, 0);

 

                              _spriteGridClass2.initTileSheetData();

                              shotRot = new BlitSprite(_tileSheet2, _spriteGridClass2.shotRotFrames, 0);

 

                              _spriteGridClass2.initTileSheetData();

                              MGRot = new BlitSprite(_tileSheet2, _spriteGridClass2.mgRotFrames, 0);

 

                              //Pistol left and right frames

 

                              _spriteGridClass1.initTileSheetData();

                              pistolRight = new BlitSprite(_tileSheet, _spriteGridClass1.PistolRightFrames, 0);

 

                              _spriteGridClass1.initTileSheetData();

                              pistolLeft = new BlitSprite(_tileSheet, _spriteGridClass1.PistolLeftFrames, 0);

 

 

                              drawCrosshairs();

                              //draw();

                    }

 

 

                    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;

                       

                      }

                     

                      if(event.keyCode == Keyboard.Q)

                      {

                              switchWeapon("minus");

                        dontShowHoldRot = false;

 

                      }

                      if(event.keyCode == Keyboard.E)

                      {

                              switchWeapon("plus");

                              dontShowHoldRot = false;

                      }

                     

                     

                    }

                    

                    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;

                      }

                     

                     

                     

 

                    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2012 Jun 13, 2012

Copy link to clipboard

Copied

Nevermind It seems to be working now, although I don't know why it wasn't before, I'll let you know if it stops working.

Thanks

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 14, 2012 Jun 14, 2012

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines