Jump to content
  • Advertisement
Sign in to follow this  
White Shade

[HELP] ActionScript 2: Prevent doubling an action when two keys are pressed together

This topic is 1337 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the very early stages of developing a platform game in Flash CS3 using ActionScript 2. Previously, I've always just instructed the player to use the WASD keys to control the player. In this game, I'm adding the possibility to use either WASD or arrow keys.

The problem that I've run into, is that when two keys that perform the same action (the right arrow and "d" key for example) the action is performed twice. How can I disable one key if the other is being pressed?

Here's the code I'm working on: http://pastie.org/9665826[1]

Any tips?

Share this post


Link to post
Share on other sites
Advertisement

The simplest solution would be to combine the checks for equivalent button actions, instead of performing them separately:

if (Key.isDown(Key.RIGHT) || Key.isDown(68)){
	_x += speed;
}

So it wouldn't matter whether one or both keys were pressed, the check would return true either way and only add the speed once.

Share this post


Link to post
Share on other sites

The simplest solution would be to combine the checks for equivalent button actions, instead of performing them separately:

if (Key.isDown(Key.RIGHT) || Key.isDown(68)){
	_x += speed;
}

So it wouldn't matter whether one or both keys were pressed, the check would return true either way and only add the speed once.

Ah, very nice. That worked like a charm, thanks!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!