Hey there, I’m currently working on a concept for animations for sprites.
At the moment I want to initialize my Player-Object which inherits following Attribute from Sprite:
ArrayList<Animation> animations;
It’s a Container for animations (accessing them via STATE Constants)
My Animation-Class is built like this:
package Graphics;
import java.awt.Image;
import java.util.ArrayList;
/**
*
* @author Nils
*/
public class Animation
{
private ArrayList<Frame> frames;
private long totalTime, currentTime;
private int frameIndex;
private boolean looping;
private Animation nextAnimation;
private Image img;
/**
* Default constructor.
*/
public Animation(Image img, boolean looping)
{
this.img = img;
frames = new ArrayList<Frame>();
this.looping = looping;
}
public void restart()
{
currentTime = frameIndex = 0;
}
/**
* Adds an image to the animation.
*
* @param i The image to be added.
* @param time The amount of time in milliseconds this image is displayed.
*/
public void addFrame(Image i, long time)
{
totalTime += time;
frames.add(new Frame(i, totalTime));
}
/**
* Returns the current image displayed.
*
* @return The current image displayed.
*/
public Image getFrame()
{
return frames.size() == 0 || isDone() ? null : frames.get(frameIndex).i;
}
public boolean isDone()
{
return looping ? false : frameIndex >= frames.size();
}
/**
* Must be called to update the animation
*
* @param deltaTime The time passed since the last call to it.
*/
public void update(long deltaTime)
{
if (frames.size() > 1)
{
currentTime += deltaTime;
if (currentTime > totalTime)
{
if (looping)
{
frameIndex = 0;
currentTime %= totalTime;
}
else
{
frameIndex++;
}
}
if (!isDone())
{
while (currentTime > frames.get(frameIndex).time)
{
frameIndex++;
}
}
}
}
public void setNextAnim(Animation anim)
{
this.nextAnimation = anim;
}
private static class Frame
{
private Image i;
private long time;
public Frame(Image i, long time)
{
this.i = i;
this.time = time;
}
}
}
but how do I render my animations without a thread?
I somehow have to send the graphics-Object from Player, to the actual Animation and exchange the Player-Graphic with the one from the animation.
How do I do this?