How would one implement KeyListener so that I can create a two-player system where one person uses ‘.’ and ‘/’ to control a character, and the other person can use the arrow keys without them interrupting each other? The way I have it now is that when one person holds down the arrow key, their character moves, but the instant you use the other player’s controls, the first person’s character stops.
How would one implement KeyListener so that I can create a two-player system where
Share
Create a
HashMap<Int,Boolean>that marks which keys are currently pressed/depressed.Then in your game loop, you can move your objects depending on if the keys are depressed in this map.
For example: