I am working on a game using only the swing and awt packages. Note I can only use the default Java libs. Meaning I have to use KeyListener. I have imported KeyListener properly, however, it is still relatively buggy. Here is a snippet of my code.
p is for Player class, I am going to set x and y to private, however, this is for simplicity
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
if(keys[KeyEvent.VK_W] || keys[KeyEvent.VK_UP]){
p.y += 5;
}
if(keys[KeyEvent.VK_S] || keys[KeyEvent.VK_DOWN]){
p.y -= 5;
}
if(keys[KeyEvent.VK_A] || keys[KeyEvent.VK_LEFT]){
p.x += 5;
}
if(keys[KeyEvent.VK_D] || keys[KeyEvent.VK_RIGHT]){
p.x -= 5;
}
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
public void keyTyped(KeyEvent e) {
}
Sadly this code has a lag to it. Of a second... Also, the motion is not a fluid... However, I don't want you to fix my code. Rather, what other methods are more clean? As well, I have worked with engines, they all seem to have an: isDown, PressedOnce, Released(However, I know how to implement this), ect.