I'm working on a 2d game in java. I have a problem using the key listener to move the character. I might be typing the code wrong or something but i need help fixing it. This is my player.java class:
import java.awt.event.*;
I'm working on a 2d game in java. I have a problem using the key listener to move the character. I might be typing the code wrong or something but i need help fixing it. This is my player.java class:
import java.awt.event.*;
put this code in your constructor: addKeyListener(this)
Rule of thumb: always addKeyListener to a class that implements the KeyListener interface.
Even though you override the methods of the KeyListener interface, if you do not addKeyListener to your class, then it does not have listeners to help the class response to events generated by the user.
Addition to that ^
Make sure you add it to the different class's constructor (JPanel I'm guessing), not the player constructor.
Also, adding "setFocusable(true)" might fix it if it still doesn't work.