I'm trying to animate a character model in my game through various movement sprites (4 directional) as the players moves in one particular direction. To do this I intended on having the game check how far the player has traveled and cycle based on discrete distance traveled (the initial distance is reset to 0 the moment the character moves). after doing some research and trying various different approaches I'm willing to admit I've hit a rock-solid wall... Here's my code (for the sake of simplicity and readability I have reduced the code to upward movement. Also have limited the cycle down to 2 images, "up1.png" and "upX.png")
PC, the class name stands for Playable Character.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class PC here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PC extends Actor
{
public static int spd = 1;
public int dstnX = 0;
public int dstnY = 0;
public static String up = "up1.png";
private boolean isMoving = true;
public void act(){
movePC();
animateMovement();
}
public void movePC(){
if(isMoving){
dstnY = getY();
dstnX = getX();
}
if(Greenfoot.isKeyDown("w")){
setLocation(getX(), getY() -spd);
}
}
public void animateMovement(){
setImage("idle1.png");
if(Greenfoot.isKeyDown("w")){
setImage("up1.png");
if(Math.sqrt(Math.pow(getX() - dstnX, 2) + Math.pow(getY() - dstnY, 2)) >= 50){
setImage("upX.png");
}
}
}
}
