0

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

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.

Edit: 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.

5
  • Just remember if you're already firing because of the one kind of event, and if so disregard the other. What's the issue exactly? Commented Sep 4, 2016 at 13:05
  • please share some of your code. What you've got so far? Commented Sep 4, 2016 at 13:06
  • I updated my question. I hope it is more clear now. Commented Sep 4, 2016 at 13:20
  • Without your code, we can only guess Commented Sep 4, 2016 at 13:25
  • But why do you need a code. It is a simple thing. When pressing mouse left, fire weapon. When pressing space on a keyboard, do the same. It works without any problems. Problem appears when pressing both mouse and a space, and it is not a bug of any kind, it just double fires. Commented Sep 4, 2016 at 13:32

1 Answer 1

1

Handle both keys with an AND

if(keyPressed["<key1>"] && keyPressed["<key2>"]) {
//Do nothing
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I did something similar. onkeyup and onmouseup i set two variables to false (spacepress and mousepress). (Not both, mousepress only on mouseup and spacepress only on keyup). And onkeydown and mousedown setting those variables to to true, and then did some checking. Returned from event if both are true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.