Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Weird game movement.

 
Alec Porter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
post a small sample program that demonstrates the problem
(just move the square in one direction)
 
Mike Peters
Ranch Hand
Posts: 67
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a wild guess. Maybe you should listen to the key pressed event instead of the key typed event.
 
Sarah Becker
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Cheers,
Sarah
 
Alec Porter
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I'm glad someone understands what I'm saying. I'm going to look into it further. Thanks.
 
Stephan van Hulst
Bartender
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic