Character Movement Bug

Hello all :slight_smile:

Ok so I’ve developed a small game and currently stuck on an annoying bug.

Bug description?
If a character is still, it reacts to the press of a movement key immediately moving in that direction (no matter what the new direction is…)
But however, once the character is in motion and a new movement key is pressed(i.e if the character moves up, and as the up key is released and the right key is released), the character faces the new direction with no movement for approximately 2, maybe 3 redraws (where 1 redraw is 80ms) before it starts moving again.

The problem I’m facing now is that if I change the update time for a redraw to something lower such as 30ms, the game operates faster, but how would I go about keeping the animation smoother without adding new images to my animation array?

Help would be greatly appreciated :slight_smile: :slight_smile:

Please provide some relevant code, it’s kinda hard to help you otherwise :stuck_out_tongue:

This sounds like a problem previously discussed on this forum. http://www.java-gaming.org/index.php/topic,23875.msg198882.html#msg198882 The OS puts a delay in, when you initially press a key. Using a boolean to keep track of the key’s state from the initial press to the key released.

Use your delta time to multiply the dx and dy. And also

If you have 4 directions key then you need 4 boolean variabels.

As said, hard to say with no example code.

I would do this: have the keylistener change a state variable (content = up, down, right, left, rest, as enums) in your moving object, probably on the key press. Have your animation code consult this state variable before computing the move. Are you doing this already?

Yeah what the others said booleans… I have an array of them, indicating which keys are pressed and which arnt. The KeyReleased then sets the booleans to false

If key release is what sets false, and the game logic goes through the booleans in sequence, that might be the cause.

Example: if booleanUp is true and you test for booleanUp before booleanDown, then, if when you keypress “down”, you will continue to go up until you release the down key and reset booleanUp to false.

That’s why I’d use a single direction variable with enums. But maybe have the keydowns rather than key releases reset ALL your booleans and this would also work.