Hi! So, I've been trying to get this sprite to animate, but unfortunately it's just going left or right. Can anybody point out anything wrong with my code? Or suggest to me a better way to make the sprite animate? I'm kind of new about the whole programming thing, and I'm really confused. Help!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* The main protagonist of this game. Should only do three things.
* Sprite should only walk LEFT, RIGHT, and BACK with animation.
*
* @MakkaChan
* @version 1.0
*/
public class Father extends Actor
{
public Father()
{
setImage("sprite_DadRight1.png");
frame = 0;
direction = 0;
gravityStrength = 3;
vSpeed = 0;
}
/**
* Act - do whatever the Father wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(Greenfoot.isKeyDown("left"))
{
moveLeft();
direction = -1;
if(onGround())
{
animateWalking();
mirror();
}
standingStill = false;
}
else if(Greenfoot.isKeyDown("right"))
{
moveRight();
direction = 1;
if(onGround())
animateWalking();
standingStill = false;
}
}
public void animateWalking()
{
if(frame == 0) {setImage("sprite_DadRight1.png");}
else if(frame == 1) {setImage("sprite_DadRight0.png");}
else if(frame == 2) {setImage("sprite_DadRight1.png");}
else if(frame == 3)
{
setImage("sprite_DadRight2.png");
frame = 0;
return;
}
frame++;
}
public void moveLeft()
{
setLocation(getX() - 10, getY());
}
public void moveRight()
{
setLocation(getX() + 10, getY());
}
public boolean onGround()
{
if(getY() > 400)
{
return true;
}
return false;
}
public boolean underGround()
{
if(getY() > 420)
{
return true;
}
return false;
}
public void mirror()
{
getImage().mirrorHorizontally();
}
private int frame;
private int direction;
private boolean standingStill;
private double gravityStrength;
private double vSpeed;
}