File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Weird game movement." Watch "Weird game movement." New topic
Author

Weird game movement.

Alec Porter
Greenhorn

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
Greenhorn

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.

Cheers,
Sarah
Alec Porter
Greenhorn

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
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Weird game movement.