Meaningless Drivel is fun!
The moose likes Game Development and the fly likes Weird game movement. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Weird game movement." Watch "Weird game movement." New topic

Weird game movement.

Alec Porter

Joined: Sep 05, 2010
Posts: 29
Hey there, I've been making a couple basic games. I've made a basic swing application and a basic applet using AWT, and I've come up with a problem. The programs have a simple square that you move with the arrow keys. It seems that when I receive keyboard events there is a half second delay when a press down a key and when the square starts to move. I want to make it so that the square instantly starts moving in a direction instead of this weird delay. Can anyone explain to me what exactly is occurring here?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
post a small sample program that demonstrates the problem
(just move the square in one direction)
Mike Peters
Ranch Hand

Joined: Oct 10, 2009
Posts: 67

Just a wild guess. Maybe you should listen to the key pressed event instead of the key typed event.

Mike Peters
Sarah Becker

Joined: Nov 14, 2010
Posts: 5
Hi Alec,

I had the same problem if you mean the a....aaaaaaaaaaaaaaaaaaaaa phenomenon (happens every time you press a key in any common program in Windows). I've heard that this is an OS -feature and there's really nothing you can do about it. Correct me if I'm wrong.
I realised this when I wanted to make my sprites jump the first time. My character just froze in the air for a while and the rest of the jump went just like it was supposed to.
I fixed this by adding a thread (you can also use the java timer) in the key pressed-method that waits a certain equal time when any key is pressed. While running is not so obvious the jumping was really disturbing and I just made the velocity (how many pixels the sprite is updated left,right,up or down) a bit faster so the delay cannot be seen. I hope this was helpful.

Alec Porter

Joined: Sep 05, 2010
Posts: 29
Thanks, I'm glad someone understands what I'm saying. I'm going to look into it further. Thanks.
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4597

Sarah, I would suggest against solving it the way you did. Instead, you could for example use a boolean value movingUp, which you set when the up key is pressed, and which you unset when it is released. Then poll this boolean to determine whether the block should be moved. The same goes for the other keys.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
I agree. Here's the link:
subject: Weird game movement.
jQuery in Action, 3rd edition