2

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

Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
  • 1
    You haven't told us enough about what's calling the method or what calls *that*, or even what platform you're using. (Is this Android? Java desktop?) More context please. – Jon Skeet Feb 08 '14 at 16:06
  • Is this method called from the GUI thread or from some background thread? – Boris the Spider Feb 08 '14 at 16:11
  • @BoristheSpider The game-loop runs inside a thread which isn't the GUI thread. And `update()` (which as I said calls `FollowPlayer()`) gets called from within the game-loop. So I think that this method runs from a 'background thread', not the GUI thread. – Aviv Cohn Feb 08 '14 at 16:16
  • Well, if you are using Swing and you are updating the GUI a thread that isn't the EDT then you are violating the Swing threading model. This can cause all sorts of weird and wonderful things to happen. – Boris the Spider Feb 08 '14 at 16:26
  • @BoristheSpider I see. I made another question about using Swing for developing games. Could you look at it and maybe answer it? Thanks http://stackoverflow.com/questions/21649280/java-using-swing-for-programming-games – Aviv Cohn Feb 08 '14 at 17:54

0 Answers0