I'm trying to make this game that involves an image icon moving across the screen depending on input from the arrow keys. I started by using Key Listeners which worked fine, but to avoid future problems with focus and maintainability, I decided to try Key Bindings because everyone recommends using Key Bindings over listeners. However, I found that Key Listeners provide smooth, continuous movement, while key bindings produce jagged animations where you can see the sprite moving instantly from one position to a given interval of pixels in some direction. Am I doing something wrong? Should I just stick to listeners?
My code for key bindings (only for moving right):
im = this.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), "right");
am = this.getActionMap();
am.put("right", new AbstractAction(){
public void actionPerformed(ActionEvent e)
{
if(x != 440)
{
x+=1;
repaint();
}
}
});
}