-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAnimation.java
More file actions
50 lines (39 loc) · 1.16 KB
/
Animation.java
File metadata and controls
50 lines (39 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package entity;
import java.awt.image.BufferedImage;
public class Animation {
private BufferedImage[] frames;
private int currentFrame;
// timer between frames
private long startTimer;
// how long between each frame
private long delay;
private boolean wasPlayedOnce;
public Animation() {
wasPlayedOnce = false;
}
public void setFrames(BufferedImage[] frames) {
this.frames = frames;
currentFrame = 0;
startTimer = System.nanoTime();
wasPlayedOnce = false;
}
public void setDelay(long delay) { this.delay = delay; }
public void setCurrentFrame(int currentFrame) { this.currentFrame = currentFrame; }
public void update() {
// handles the logic for determining whether or not move to the next frame
if(delay == -1) return;
long elapsed = (System.nanoTime() - startTimer) / 1000000;
if(elapsed > delay) {
currentFrame++;
startTimer = System.nanoTime();
}
if(currentFrame == frames.length) {
currentFrame = 0;
wasPlayedOnce = true;
}
}
// getters
public int getFrame() { return currentFrame; }
public BufferedImage getImage() { return frames[currentFrame]; }
public boolean hasPlayedOnce() { return wasPlayedOnce; }
}