This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

I'm having trouble with my game...

 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kevin Glass
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Micah Pezdirtz
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic