I'm making a 2D game in Java. I have a specific method that moves an AI spaceship around the screen towards a specific radius from the player's spaceship. (Let's call this method FollowPlayer()
).
Most of the time this works fine. FollowPlayer()
gets called in time intervals ranging from 15 to 16 milliseconds.
But once in a while, this specific method stats getting called every 3000 milliseconds (!) instead of 15 milliseconds. This obviously causes real weird behavior in the game.
(The graphics stay smooth, since the update rate of the drawing stays consistent. Only this specific method, which if necessary changes the AI's direction of motion, begins to run every three seconds instead of every 15 milliseconds, thus the AI reacts to some things in a big delay).
The FollowPlayer()
method is called from within the update()
method of the AiSpaceShip
class. The update()
method is called every frame, aka 50 times per second, so FollowPlayer()
should always run 50 time per second.
Any clue as to what could cause this weird behavior? I'm getting pretty desperate.
The platform is Java, on a desktop computer. I'm using Swing.
Thanks