Created
January 3, 2015 21:30
-
-
Save darkwave/05421802e9d5ab9ba550 to your computer and use it in GitHub Desktop.
Simple Animated sprite
This file contains hidden or 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
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