// inspired by http://www.processing.org/learning/topics/animatedsprite.html // Class to keep track of an animation, namely what frame to draw and when to change to the next frame // The program should call the Animate() function (from the Draw() function or some other timed event) // to make the animation timer increase class Animation { // Frames object holding the animation frames Frames frames; // The current frame to be drawn, initialized as 0 int currFrame = 0; // The delay between frames, initialized as 0 int delayTime = 0; // The timer counting how many times the current frame has been drawn, initialized as 0 int animTimer = 0; int x; // Constructor // Similar to the Frames constructor with the addition of: // delayTime - the delay between frames Animation(String filePrefix, String fileSuffix, int numFrames, int delayTime) { // store the delay time for this animation this.delayTime = delayTime; // load the frames frames = new Frames(filePrefix, fileSuffix, numFrames); } // (Another) Constructor, encourages several actors to share the same Frames object, // so that copies of the same images are not repeatedly loaded into memory, for each Animation // frames - a Frames object containing the animation frames // delayTime - the delay between frames Animation(Frames frames, int delayTime) { this.delayTime = delayTime; this.frames = frames; } // This function should be called every time from the Draw() function // or some other timed event, driving the animation clock void animate() { // increase the timer animTimer++; // if enough time was spent on this frame if (animTimer >= delayTime) { // reset the timer animTimer = 0; // advance the frame currFrame++; // check for an invalid frame index, reset to zero (makes the animation loop) if ( currFrame >= frames.size() ) currFrame = 0; } } // Draw the current frame image at a specific coordinate (x,y) // Depends on the current Processing imageMode() void paint(int x, int y) { image(frames.images[currFrame], x, y); } }