-
Notifications
You must be signed in to change notification settings - Fork 244
/
Animaciones.java
54 lines (45 loc) · 1.15 KB
/
Animaciones.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
Animation animation1, animation2;
float xpos;
float ypos;
float drag = 30.0;
void setup() {
size(640, 360);
background(255, 204, 0);
frameRate(24);
animation1 = new Animation("PT_Shifty_", 38);
animation2 = new Animation("PT_Teddy_", 60);
ypos = height * 0.25;
}
void draw() {
float dx = mouseX - xpos;
xpos = xpos + dx/drag;
// Display the sprite at the position xpos, ypos
if (mousePressed) {
background(153, 153, 0);
animation1.display(xpos-animation1.getWidth()/2, ypos);
} else {
background(255, 204, 0);
animation2.display(xpos-animation1.getWidth()/2, ypos);
}
}
class Animation {
PImage[] images;
int imageCount;
int frame;
Animation(String imagePrefix, int count) {
imageCount = count;
images = new PImage[imageCount];
for (int i = 0; i < imageCount; i++) {
// Use nf() to number format 'i' into four digits
String filename = imagePrefix + nf(i, 4) + ".gif";
images[i] = loadImage(filename);
}
}
void display(float xpos, float ypos) {
frame = (frame+1) % imageCount;
image(images[frame], xpos, ypos);
}
int getWidth() {
return images[0].width;
}
}