potatoguy123
New member
I'm writing a game, and I have one playable character and one NPC on the screen (I'm still creating, testing). They are both being constantly drawn to the screen (using a primary thread that continually draws them). The NPC object's class itself is a thread, which continuously moves the character and sleeps for half a second.
I use a timer for the animation and movement of both the player and the NPC. The timer just counts to 16 (the tile size), moving the character each time and changing the sprite accordingly.
My problem is that when both characters are moving at the same time, they both go slowly. I'm pretty sure that this is because of the clash between the two timers in the two threads. Since my NPC stops for half a second, my player moves at different speeds, slowing down and speeding up randomly.
SO, my question to all of you, is this: is there an easy way to to animate a sprite without using javax.swing.Timer? Will the java.util.Timer work any better? Can I do this with a dedicated thread?
Sorry I'm not posting code. This is more of a concept question than a code error. Thanks, guys.
Sorry, I should really do some more random button mashing before I post questions here. I got it to work well by replacing the timer with a for loop, in which the NPC's thread sleeps for 10 ms after moving. Now both characters move at the same speed, and not at a snail's pace.
But still, if you have a better way of animating them simultaneously, I'm still open to suggestions.
I use a timer for the animation and movement of both the player and the NPC. The timer just counts to 16 (the tile size), moving the character each time and changing the sprite accordingly.
My problem is that when both characters are moving at the same time, they both go slowly. I'm pretty sure that this is because of the clash between the two timers in the two threads. Since my NPC stops for half a second, my player moves at different speeds, slowing down and speeding up randomly.
SO, my question to all of you, is this: is there an easy way to to animate a sprite without using javax.swing.Timer? Will the java.util.Timer work any better? Can I do this with a dedicated thread?
Sorry I'm not posting code. This is more of a concept question than a code error. Thanks, guys.
Sorry, I should really do some more random button mashing before I post questions here. I got it to work well by replacing the timer with a for loop, in which the NPC's thread sleeps for 10 ms after moving. Now both characters move at the same speed, and not at a snail's pace.
But still, if you have a better way of animating them simultaneously, I'm still open to suggestions.