Note that there are some explanatory texts on larger screens.

plurals
  1. POSprite Animation Management Using Threads
    primarykey
    data
    text
    <p>I'm in the process of creating my own two-dimensional game. Every object in the game world has a Sprite object, and when the screen is drawn, the object's sprite is drawn in the object's location.</p> <p>The sprite class can be either a single image or a series of images used to make an animates sprite.</p> <pre><code>import java.awt.image.BufferedImage; import java.awt.Graphics2D; public class Sprite implements Runnable{ private int currentImage; private BufferedImage[] sprites; private int delayMS; private Thread animation; public Sprite(BufferedImage sprite){ sprites = new BufferedImage[1]; sprites[0] = sprite; } public Sprite(BufferedImage[] spriteAnimation,int delay){ this.sprites = spriteAnimation; currentImage = 0; delayMS = delay; //start a thread to time animation animation = new Thread(this); animation.start(); } private void next(){ if(currentImage &lt; sprites.length - 1) currentImage++; else currentImage = 0; } public void run() { while (Thread.currentThread() == animation) { //delay the animation for delayMS try { Thread.sleep(delayMS); } catch (InterruptedException e) { break; } //next image next(); } } public void draw(Graphics2D g,int x,int y){ g.drawImage(sprites[currentImage],null,x,y); } } </code></pre> <p>I couldn't find any reliable information on the subject of using threads to run many animations, and I was wondering whether this was the best approach. Everything works great until I throw 200ish identical objects into the world. FPS begins to lower and some animated sprites begin to change frames at different times. This would make sense since the threads would start to delay when instantiated.</p> <p>My questions is whether there is a more efficient way to handle this, allowing me to use more objects without significant FPS loss, and synchronize the thread/threads so that the animations switch frames together.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload