Skip to content

Instantly share code, notes, and snippets.

@darkwave
Created January 3, 2015 21:30
Show Gist options
  • Save darkwave/05421802e9d5ab9ba550 to your computer and use it in GitHub Desktop.
Save darkwave/05421802e9d5ab9ba550 to your computer and use it in GitHub Desktop.
Simple Animated sprite
PAnimation front, up, side;
void setup() {
//size(640, 480);
front = new PAnimation("front.png", 37, 90, 100);
up = new PAnimation("up.png", 40, 90, 100);
side = new PAnimation("side.png", 53, 90, 100);
}
void draw() {
background(0);
front.display();
translate(0, 90);
up.display();
translate(0, 90);
scale(3);
side.display();
}
class PAnimation {
PImage spriteSheet;
int animationLength, w, h;
float speed = 1000;
int lastFrame = 0;
int animCounter = 0;
PAnimation(String filename, int spriteWidth, int spriteHeight, float animationSpeed) {
spriteSheet = loadImage(filename);
w = spriteWidth;
h = spriteHeight;
animationLength = spriteSheet.width / w;
speed = animationSpeed;
}
void display() {
if (millis() - lastFrame > speed) {
animCounter++;
animCounter %= animationLength;
this.lastFrame = millis();
}
int uv = w * animCounter;
image(spriteSheet, 0, 0, w, h, uv, 0, uv + w, h);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment