However, to my knowledge this would load a duplicate set of image files every time I create a new object, which doesn't sound like a very good idea. This is the code for my simple Animation Manager and the container classes it uses to store the frames:
import java.util.*;
import java.awt.image.*;
public class AnimationManager {
private ArrayList<Animation> animationList; //dynamic array of Animation(s)
private int currentAnimation;
private int currentFrame;
private boolean isPaused;
public AnimationManager() {
currentAnimation = 0;
currentFrame = 0;
isPaused = false;
}
public void Update() {
currentFrame++;
if (currentFrame >= animationList.get(currentAnimation).GetNumberOfFrames()) {
currentFrame = 0;
}
}
public void AddNewAnimation(Animation new_animation) {
animationList.add(new_animation);
}
public BufferedImage GetCurrentImage() {
return animationList.get(currentAnimation).GetFrame(currentFrame).image;
}
public void ChangeAnimation(int new_currentAnimation) {
currentAnimation = new_currentAnimation;
currentFrame = 0;
}
public void Start() {
isPaused = false;
}
public void Stop() {
isPaused = true;
currentFrame = 0;
}
public void Pause() {
isPaused = true;
}
}
public class Animation {
private Frame frame[];
public Animation(SpriteSheet new_sheet) {
frame = new Frame[new_sheet.sprite.length]; //Create an array just big enough to hold all the sprites in the sprite sheet.
for (int index = 0; index < frame.length; index++) { //For as many Frames as exist, load the corresponding image from the sprite sheet.
frame[index].image = new_sheet.sprite[index];
}
}
public Frame GetFrame(int frameNumber) {
return frame[frameNumber];
}
public int GetNumberOfFrames() {
return frame.length;
}
public void AddKeyFrame(int frameNumber, String new_keyFrame) {
frame[frameNumber].isKeyFrame = true;
frame[frameNumber].keyFrame = new_keyFrame;
}
}
import java.awt.image.*;
public class Frame {
public BufferedImage image;
public boolean isKeyFrame;
public String keyFrame; //Identifies the action to be performed when this frame is displayed.
public Frame(BufferedImage new_image) { //Non-KeyFrame Constructor
image = new_image;
isKeyFrame = false;
keyFrame = "";
}
public Frame(BufferedImage new_image, String new_keyFrame) { //KeyFrame Constructor
image = new_image;
isKeyFrame = true;
keyFrame = new_keyFrame;
}
}
How would I go about storing a single image that will be used by all instances of a given entity? How can I avoid a central repository that hands a reference out upon request?
I have a strong feeling that the static keyword should come into play here, but despite reading probably a dozen tutorials by now on how to use it, I'm still not sure how it might work. I mean, I get the concept of where you want to use it, but not quite the how.
Or perhaps I structured myself into a hole with the way I wrote it? This is my first attempt at animating sprites, and I'm building off of concepts I've learned from reading around, but I'm trying to avoid seeing actual code until I either get a system working myself or run up against a brick wall.

New Topic/Question
Reply



MultiQuote









|