Skip to main content
Inspiring
February 3, 2008
Question

Moving Sprite with arrow keys, simultaneous directions

  • February 3, 2008
  • 2 replies
  • 476 views
Hi everyone.

I'm trying to create a small arcade style game, you control a small ship, shoot at incoming targets, try to dogde their shots, etc. I'm working on the ship's movement right now, using the arrow keys to control it. Getting it to move one direction at a time is easy, but sometimes I need to move it diagonally, otherwise I think gameplay will suffer considerably.

I have tried several things but I never seem to be able to tell Flash that two keys are being pressed simultaneously. Can anyone tell me how I might achieve this?

Thank you.
This topic has been closed for replies.

2 replies

Participating Frequently
February 3, 2008
PS You would then reference the variables in the moveShip function.
Participating Frequently
February 3, 2008
This format should allow detection of multiple keys at once:

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

function keyPressed(ev:KeyboardEvent):void {
var pressedKey:uint = ev.keyCode;

if(pressedKey == Keyboard.LEFT) {
leftDown = true;
}
if(pressedKey == Keyboard.RIGHT) {
rightDown = true;
}
if(pressedKey == Keyboard.UP) {
upDown = true;
}
if(pressedKey == Keyboard.DOWN) {
downDown = true;
}
ship.addEventListener(Event.EnterFrame, moveShip);
}