lluxx lluxx - 1 year ago 52
Javascript Question

javascript (html5) -Can I disable two keys from firing at the same time, simultaneously?

I am making a game. I want to enable firing from either left mouse button or space on a keyboard.

I don't want to allow double firing. By fast pressing both space and left mouse, I get double power. I played some desktop games, where firing is the same, regardless of pressing both keys or just one.

When I press both space and left mouse, code gets double executed.
Ok, it could be that it is not double executed, it could be that I am firing really fast when using both buttons, but I don't understand how some desktop games, for example chicken invaders implemented it so it fires always the same amount of projectiles, either pressing space or mouse or both mouse and space. even when you are firing really fast.

Answer Source

Handle both keys with an AND

if(keyPressed["<key1>"] && keyPressed["<key2>"]) {
//Do nothing