Loading...
Logo
Processing Forum

Keyboard problem

in Programming Questions  •  2 months ago  
Hello!
Why can't I press more than one button at the same time?
If I press the UP button, the player goes up (y--)
If I press the LEFT button, the player goes left (x--)
But if I press them both the player does not go diagonal, but only in one direction.
This is the buttons code:

Copy code
  1. if (keyPressed)
  2. {
  3.   if (keyCode == LEFT)
  4.   {
  5.     x--;
  6.   }
  7.   if (keyCode == UP)
  8.   {
  9.     y--;
  10.   }
  11. }

Replies(1)

Re: Keyboard problem

2 months ago
keyCode is just a vanilla int variable. And as such, can hold up 1 value only.
Which is the latest key pressed or released.

There are some algorithms to deal w/ multiple key presses.
Check it out a technique which uses keyPressed() + keyReleased() to set true/false 4 boolean variables:

http://studio.processingtogether.com/sp/pad/export/ro.9bY07S95k2C2a/latest
http://studio.processingtogether.com/sp/pad/export/ro.9ck0KLYubLcZG/latest