This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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.
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
Joined: Aug 01, 2006
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.
Joined: Aug 14, 2006
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.
Joined: Aug 01, 2006
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: