aspose file tools*
The moose likes Game Development and the fly likes I'm having trouble with my game... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "I Watch "I New topic
Author

I'm having trouble with my game...

Micah Pezdirtz
Greenhorn

Joined: Aug 14, 2006
Posts: 25
My problem is that in my game, when i move the character sprite, I'm having the characters sprite repositioned acorrdingly in the keyPressed() method, but the result is not what I want. When you press the arrow key, he moves over a bit, then pauses, then starts moving like you would expect. I know why this is happening, but I dont know what to do to make it so when I press down the arrow key the character starts moving smoothly in the appropriate direction. I'm not sure, but something makes me think I read about a keyDown() method...
Anybody know what I'm talking about and how I can fix it?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

This is happening because the OS has a built in repeat delay mechanism that you need to override. I don't know how to fix this and I've read you can't do it with pure java but I know some gaming frameworks like JMonkeyEngine, SDL, LWJGL, and JOGL have found away around this, probably at the OS level with some JNI.

Sorry that this isn't very helpful but I thought I could at least point you in a direction. I did find this thread which may be a bit more helpful.


GenRocket - Experts at Building Test Data
Kevin Glass
Greenhorn

Joined: Aug 01, 2006
Posts: 4
Hey Micah,

What you generally do is set a flag in keyPressed() rather than actually moving your character in the method. So something like


and then in the bit of your code where you're looping to render the graphics and update the game you do something like:


This prevents you relying on the key repeat mechanism of the operating system.

Hope this helps,

Kev

[ August 14, 2006: Message edited by: Kevin Glass ]
[ August 14, 2006: Message edited by: Kevin Glass ]

<a href="http://www.cokeandcode.com" target="_blank" rel="nofollow">Blog / Games / Tutorials</a><br /><a href="http://www.tiltilation.com" target="_blank" rel="nofollow">Current Game</a>
Micah Pezdirtz
Greenhorn

Joined: Aug 14, 2006
Posts: 25
great! i was actually thinking of similar idea, in keyPressed() i could set it in motion then in keyReleased() stop it. thanks alot, i apreciate(sp?) your help and now i know that this way is good.
too bad for me my luck ridiculously terrible and i am bound to have worse errors lol
Kevin Glass
Greenhorn

Joined: Aug 01, 2006
Posts: 4
Nature of game development I'm afraid. Move from one head banging against the wall problem to the next.

It's part of the fun!

Be sure to check back if you have more problems, I'm sure the nice people here can/will help you out.

Kev
Micah Pezdirtz
Greenhorn

Joined: Aug 14, 2006
Posts: 25
thanks for the support, i actually get alot of those problems that make you feel like and then when you find the solution you feel like again for not seeing how simple it was.
your advice is making it work great now, but if i'm holding one direction, then press another while still holding, the movment will behave as i described before. can i get around this as well? in the keyListener should i check for simultaneous buttons being pressed at once? or make it so that pressing another button while the one is already being pressed do nothing? this inst very important, but i want a higher understanding of my own program.
Kevin Glass
Greenhorn

Joined: Aug 01, 2006
Posts: 4
What platform are you on? On linux I've seen some oddities with key presses reporting pressed then released for key repeat - which would give a similar experience.

Here's a utility class I wrote a long time ago for Keyboard control - might help you out here:

http://www.cokeandcode.com/info/showsrc/showsrc.php?src=../spaceinvaders103/org/newdawn/spaceinvaders/util/Keyboard.java

Kev
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I'm having trouble with my game...