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?
Joined: Jun 09, 2003
post a small sample program that demonstrates the problem
(just move the square in one direction)
Just a wild guess. Maybe you should listen to the key pressed event instead of the key typed event.
Joined: Nov 14, 2010
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.
Joined: Sep 05, 2010
Thanks, I'm glad someone understands what I'm saying. I'm going to look into it further. Thanks.
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.