Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.util.ArrayList;
- import java.util.List;
- public class Animation {
- private int frameCount; // Counts ticks for change
- private int frameDelay; // frame delay 1-12 (You will have to play around with this)
- private int currentFrame; // animations current frame
- private int animationDirection; // animation direction (i.e counting forward or backward)
- private int totalFrames; // total amount of frames for your animation
- private boolean loop; // loop this?
- private boolean stopped; // has animations stopped
- private List<Frame> frames = new ArrayList<Frame>(); // Arraylist of frames
- public Animation(BufferedImage[] frames, int frameDelay, boolean shouldLoop) {
- this.frameDelay = frameDelay;
- this.stopped = true;
- this.loop = shouldLoop;
- for (int i = 0; i < frames.length; i++) {
- addFrame(frames[i], frameDelay);
- }
- this.frameCount = 0;
- this.frameDelay = frameDelay;
- this.currentFrame = 0;
- this.animationDirection = 1;
- this.totalFrames = this.frames.size();
- }
- public void start() {
- if (!stopped) {
- return;
- }
- if (frames.size() == 0) {
- return;
- }
- stopped = false;
- }
- public void stop() {
- if (frames.size() == 0) {
- return;
- }
- stopped = true;
- }
- public void restart() {
- if (frames.size() == 0) {
- return;
- }
- stopped = false;
- currentFrame = 0;
- }
- public void reset() {
- this.stopped = true;
- this.frameCount = 0;
- this.currentFrame = 0;
- }
- private void addFrame(BufferedImage frame, int duration) {
- if (duration <= 0) {
- System.err.println("Invalid duration: " + duration);
- throw new RuntimeException("Invalid duration: " + duration);
- }
- frames.add(new Frame(frame, duration));
- currentFrame = 0;
- }
- public BufferedImage getSprite() {
- return frames.get(currentFrame).getFrame();
- }
- public void update() {
- if (!stopped) {
- frameCount++;
- if (frameCount > frameDelay) {
- frameCount = 0;
- currentFrame += animationDirection;
- if (currentFrame > totalFrames - 1) {
- currentFrame = 0;
- if(!loop){
- stopped = true;
- }
- }
- else if (currentFrame < 0) {
- currentFrame = totalFrames - 1;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement