0

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();
            }
        }
    });
}
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Mar 24 '19 at 04:40
  • An image wouldn't help, I would need a video. – Josh Blecherman Mar 24 '19 at 04:58
  • *"An image wouldn't help"* You mistook what I meant by 'hot link to an image'. You mention *"an **image** icon"* is used by the code. Here is an [example of hot linking to an image](https://stackoverflow.com/a/6296381/418556) in the first two lines of the `main` method. That image happened to be embedded in the question itself. – Andrew Thompson Mar 24 '19 at 05:15
  • BTW - I resisted voting to close this question when I made the first comment. But since you have obviously seen it and failed to post an MCVE, I added a close vote. – Andrew Thompson Mar 24 '19 at 05:31
  • I don't see any difference in the animation. See [Motion Using the Keyboard](https://tips4java.wordpress.com/2013/06/09/motion-using-the-keyboard/) for working examples of both approaches. – camickr Mar 24 '19 at 21:00

1 Answers1

0

IIRC key binds behave just like when you type into a text field. Specifically, I mean that if you hold a button (in this case, the right arrow key), it will send the keypress once and after a second or two begin to send the keypress rapidly. That is why you will get choppy movement, the game loop runs more often than the bind is called.

This is why it is better to poll the keys with a listener each time the main loop is run because then the animation will only ever get as choppy as the loop itself. And if that loop gets choppy, the game is just running slowly in its entirety.