Hi, I’m ChouChou, I’ve been lurking around this great forum for a while and this is my first post.
I have a question regarding the character states and animation, I’ ve read some great posts from
http://www.java-gaming.org/index.php/topic,21788.0.html
and
http://www.java-gaming.org/index.php/topic,16829.0.html
I’m doing a side scroller game like rockmanX4(using Zero’s light saber as weapon) and hopefully the control/animation will be smooth like it in the future, but I felt like I coded my character states and animation class wrong as I need a bunch of if statements just to get things working. Following is the code and I’m seeking advice on how to improve my state/animation system to make things better, thanks in advance!
P.S I will post additional code if required.
States Class
public class States {
/** Character actions */
public static final int IDLE = 0;
public static final int RUNNING = 1;
public static final int JUMPING = 2;
public static final int HIT = 3;
public static final int ATTACKING = 4;
/** Directoin player is facing */
public static final int LEFT = -1;
public static final int RIGHT = -2;
}
Animation Class
public class Animation {
private BufferedImage img;
private int frameDuration = 60;
private int frameNumberY, frameNumberX, frameX, frameY, charWidth, charHeight, sheetWidth, sheetHeight, playStyle;
private long lastFrameChange;
private long lastLoopTime = System.currentTimeMillis();
/** Boolean to check if an animation has finished playing */
private boolean isFinished = false;
/** Play animation once */
public static final int PLAY_ONCE = 0;
/** Loop animation */
public static final int LOOPING = 1;
/** Entity's animation current state */
private int currentState = States.IDLE;
/** Current playing animation state */
private int animState = States.IDLE;
/** Creates animation for entity*/
public void createAnimation(BufferedImage img, int x, int y, int offsetX, int offsetY, int charWidth, int charHeight,
int sheetWidth, int sheetHeight, int frameDuration, int startFrame, int playStyle) {
if(currentState > animState || currentState == States.IDLE) {
animState = currentState;
frameNumberX = 0;
lastFrameChange = 0;
this.img = img;
this.charWidth = charWidth;
this.charHeight = charHeight;
this.sheetWidth = sheetWidth;
this.sheetHeight = sheetHeight;
this.frameDuration = frameDuration;
this.playStyle = playStyle;
}
}
public void update() {
if(currentState == animState && !isFinished) {
/** Difference between current time and last loop time */
long delta = System.currentTimeMillis() - lastLoopTime;
/** Last loop time */
lastLoopTime = System.currentTimeMillis();
/** Variable to check if frame > duration */
lastFrameChange += delta;
/** Change frame */
if (lastFrameChange > frameDuration) {
/** Reset frame */
lastFrameChange = 0;
/** update the frame X */
frameNumberX++;
if (frameNumberX >= sheetWidth) {
if(playStyle == PLAY_ONCE) {
isFinished = true;
currentState = States.IDLE;
animState = States.IDLE;
} else if(playStyle == LOOPING) {
frameNumberX = 0;
}
}
/** update the frame Y */
frameNumberY++;
if (frameNumberY >= sheetHeight) {
frameNumberY = 0;
}
}
/** Current frame X */
frameX = (frameNumberX % sheetWidth) * charWidth;
/** Current frame Y */
frameY = (frameNumberY % sheetHeight) * charHeight;
}
/** When animation finished change states to IDLE*/
if(isFinished) {
isFinished = false;
}
}
/** Get current image */
public BufferedImage getCurrentImage() {
return img.getSubimage(frameX, frameY, charWidth, charHeight);
}
/** Set playing animation state */
public int getAnimState() {
return animState;
}
/** Get current entity animation state */
public int getCurrentState() {
return currentState;
}
/** Set entity animation state */
public void setCurrentState(int state) {
this.currentState = state;
}
/** Set entity animation state */
public void setAnimState(int state) {
this.animState = state;
}
/** Return if animation has finished playing or not */
public boolean isFinished() {
return isFinished;
}
public void setFinished(boolean isFin) {
isFinished = isFin;
}
}
