// Sprite.java import javax.microedition.lcdui.Graphics; /** * Thread that sends players World x and y coordinates * and receives other players coordinates back in response. * * @author Jack Wootton * @version Last modified 10/03/05 **/ public class Sprite { private int currentFrame; private int currentState; private long currentStateBegan; // time this currentState started private ImageSet imageSet; private long lastFrameChange; /** * Constructor for a Sprite object requiring the source image set and the * starting state and frame. * @param is The imageSet which is the source of graphics for this Sprite. * @param startingState The starting state (normally 0). * @param startingFrame The starting frame (normally 0). */ public Sprite(ImageSet is, int startingState, int startingFrame) { imageSet = is; setState(startingState, true); currentFrame = startingFrame; } /** * Change to a specific frame. * @param f The frame to change to. */ public final void setFrame(int f) { currentFrame = f; } /** * Change to a different state. * @param s The state to change to. * @param force Normally we wont change change if that is already the current * state. However this has the effect of not reseting the state began time * and totalCycles counter. Set this to true to force those to be reset. */ public final void setState(int s, boolean force) { if (currentState != s || force) { currentState = s; currentFrame = 0; } } /** * Resets all state information such as the current animation frame, total * number of completed cycles and the time the state began. */ public final void reset() { currentFrame = 0; lastFrameChange = 0; } /** * @return The current state. */ public final int getCurrentState() { return currentState; } /** * @return The current frame number. */ public final int getCurrentFrame() { return currentFrame; } public void setState(int s) { currentState = s; } /** * Draws the current sprite frame onto a specified graphics context. * @param target The target to draw the image frame onto. * @param targetX The target x position. * @param targetY The target y position. */ public final void draw(Graphics target, int x_, int y_) { imageSet.draw(target, currentState, currentFrame, x_, y_); } /** * Cycles the current sprites animation and goes forward by the number of * frames corresponding to the amount of time that has elapsed. * @param deltaMS The amount of time that has passed in milliseconds. */ public final void cycle() { currentFrame++; if (currentFrame >= imageSet.getTotalFrames(currentState)) { currentFrame = 0; } } }