-
Notifications
You must be signed in to change notification settings - Fork 6
/
animatedObject.java
146 lines (106 loc) · 3.94 KB
/
animatedObject.java
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Timer;
import java.io.*;
/**
* Write a description of class animatedObject here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class animatedObject extends SmoothMover
{
/**
* Act - do whatever the animatedObject wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public long deltaTime;
public long lastFrame = System.nanoTime();
public long currentFrame = System.nanoTime();
public GreenfootImage[] previousSprites = null;
public int frame = 0;
//In milliseconds (ms)
public GreenfootImage[] importSprites(String filename, int frames) {
GreenfootImage[] temp = new GreenfootImage[frames];
for (int i = 0; i < frames; i++) {
GreenfootImage scale = new GreenfootImage(filename+(i+1)+".png");
temp[i] = scale;
}
return temp;
}
public GreenfootImage[] importSprites(String filename, int frames, double scaleFactor) {
GreenfootImage[] temp = new GreenfootImage[frames];
for (int i = 0; i < frames; i++) {
GreenfootImage scale = new GreenfootImage(filename+(i+1)+".png");
scale.scale((int)(scale.getWidth()*scaleFactor), (int)(scale.getHeight()*scaleFactor));
temp[i] = scale;
}
return temp;
}
public void animate(GreenfootImage[] sprite, long duration) {
currentFrame = System.nanoTime();
deltaTime = (currentFrame - lastFrame) / 1000000;
if (deltaTime > duration) {
lastFrame = currentFrame;
frame++;
if (frame < sprite.length) {
setImage(sprite[frame]);
}
}
if (frame < sprite.length) {
} else {
frame = 0;
setImage(sprite[frame]);
}
if (!sprite.equals(previousSprites)) {
setImage(sprite[frame]);
previousSprites = sprite;
}
}
public void animate(GreenfootImage[] sprite, long duration, boolean loop) {
currentFrame = System.nanoTime();
deltaTime = (currentFrame - lastFrame) / 1000000;
if (deltaTime > duration) {
lastFrame = currentFrame;
frame++;
if (frame < sprite.length) {
setImage(sprite[frame]);
}
}
if (frame < sprite.length) {
} else if (loop) {
frame = 0;
setImage(sprite[frame]);
} else {
//do nothing
}
if (!sprite.equals(previousSprites)) {
if (frame < sprite.length) {
setImage(sprite[frame]);
previousSprites = sprite;
}
}
}
public void setFrame(int toFrame) {
frame = toFrame-1;
}
public int getCurrentFrame() {
return frame+1;
}
public void hitFlash(GreenfootImage[] sprite, String filename) {
int tempFrame = frame;
int flashFrame = tempFrame+1;
if (tempFrame >= sprite.length) {
tempFrame = 0;
flashFrame = 1;
} else if (flashFrame >= sprite.length) {
flashFrame = 0;
}
GreenfootImage first = new GreenfootImage("flash"+filename+(tempFrame+1)+".png");
GreenfootImage second = new GreenfootImage("flash"+filename+(flashFrame+1)+".png");
sprite[tempFrame] = first;
sprite[flashFrame] = second;
setImage(sprite[tempFrame]);
Timer timer = new Timer();
timer.schedule(new Timer1(sprite, filename, tempFrame,flashFrame), 500L);
}
}