multiple key detection for KeyListener (java)

后端 未结 2 1513
时光取名叫无心
时光取名叫无心 2021-01-26 23:39

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

2条回答
  •  时光取名叫无心
    2021-01-26 23:55

    Create a HashMap 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:

    if (keyMap.get(VK_COLON) == Boolean.TRUE) //True indicates pressed
       playerAXPos+= 10;
    

提交回复
热议问题