Help with animation loading all frames at once?
in
Programming Questions
•
11 months ago
So, for a project I have managed to piece together an animation. However, when a function is triggered, all the frames of each animation (most noticeable johnwalk and johndrag) seem to load at once, with the exception of "johnstill", which is under void Draw(). The following is my code.
Any help would be sincerely appreciated.
- - -
Animation johnstill, johnwalk, johnclick, johndrag;
Any help would be sincerely appreciated.
- - -
Animation johnstill, johnwalk, johnclick, johndrag;
PImage johnbg;
float xpos;
float ypos;
int flag = 0;
void setup() {
size(800, 550);
imageMode(CENTER);
frameRate(2);
johnbg = loadImage("johnbg.png");
johnstill = new Animation("johnstill_", 24);
johnwalk = new Animation("johnwalk_", 8);
johnclick = new Animation("johnclick_", 3);
johndrag = new Animation("johndrag_", 3);
}
void draw() {
background(johnbg);
ypos = mouseY;
xpos = mouseX;
if (flag == 0) {
johnstill.display(xpos, ypos);
}
}
// MOUSE MOVE FUNCTIONS.
void mouseMoved(){
flag = 1;
johnwalk.display(xpos, ypos);
flag = 0;
}
void mousePressed(){
if (mousePressed == true) {
johnclickdisplay(xpos, ypos);
}
}
void mouseDragged(){
flag = 1;
johndrag.display(xpos, ypos);
flag = 0;
}
// ANIMATION CODING.
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++) {
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;
}
}
1