I am creating a simple game in which a keypressed event moves a JLabel
a certain number of pixels in a JPanel
.
It works without any errors and when i hit the right arrow it moves right, and so on. However, as soon as i press a second key it interrupts the original movement.
For example if i was holding down the left key (and the object was moving left) and then i hit the up arrow the object stops moving left. This is (of course) because i am using only one key event for all my key.
This obviously is not an acceptable situation because as soon as i add a second object (for another player) each player will be constantly stopping the other players movement.
So my question is this: How best can i incorporate multiple simultaneous key actions? (preferably without writing dozens of KeyAdapter
's and KeyEvent
's)
INFO: currently i have a second class which extends a KeyAdapter
. This second class includes a KeyPressed
function which then uses .getKeyCode();
to find out which key is pressed and act.
EDIT: any suggestions are welcome!