Created
February 12, 2017 10:22
-
-
Save coolspeed/119757c97eb496a724a0e07dacf237cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Animated Sprite (Shifty + Teddy) by James Paterson. | |
Press the mouse button to change animations. Demonstrates loading, displaying, | |
and animating GIF images. It would be easy to write a program to display | |
animated GIFs, but would not allow as much control over the display sequence and | |
rate of display. | |
*/ | |
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 for animating a sequence of GIFs | |
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment