Im trying to make a soldier unit that has animation, which sort of works but when running the animation, for every other act, the actor goes invisible and also it wont change direction and flip even though i have code for it to do so
Superclass
Subclass
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Soldier here.
*
* @author (Prayan Jegathees)
* @version (0.1)
*/
public abstract class Soldier extends Actor
{
/**
* Private variables used for all soldier subclasses
* stats needed to determine the overall stats
* of the character
*
* LIFE is a boolean meant to change & detect the actors "death"
* TEAM is a string meant to choose which alliance the actor belongs to
*/
protected int health;
protected int speed;
private int atkPower;
private int atkRate;
private boolean life;
protected String team;
protected boolean withEnemy = false;
protected double vX;
protected double vY;
protected int direction;
protected SimpleTimer timer = new SimpleTimer();
/**
* Constructor for all Soldiers
*/
public Soldier(int hp, int spd, int aPwr, int aRt, String alliance)
{
health = hp;
speed = spd;
atkPower = aPwr;
atkRate = aRt;
alliance = team;
life = true;
if (alliance == "Red")
{
direction = -1;
}
else if (alliance == "Blue")
{
direction = 1;
}
}
/**
* Attack - To deal damage to an enemy soldier unit
*/
public abstract void attack();
/**
* takeDamage - When soldier subunits are hit by projectiles and lose health
* (amount taken varies based on the type of projectile)
*/
public void takeDamage()
{
/*
if (isTouching(PROJECTILE_ARROW.class))
{
health -= 2;
}
if (isTouching(PROJECTILE_FIREBL.class))
{
health -= 5;
}
*/
if (health == 0)
{
life = false;
}
}
public void die()
{
if (!life)
{
move(0);
getImage().setTransparency(50);
setRotation(90);
}
}
public void remove()
{
getWorld().removeObject(this);
}
}* Write a description of class Knight here.
*
* @author (Prayan Jegathees)
* @version (0.1)
*/
public class Minotaur extends Soldier
{
private int imgNum = 1;
private boolean animationComplete = false;
GreenfootImage [] walkImages = new GreenfootImage[5];
GreenfootImage [] atkImages = new GreenfootImage[4];
public Minotaur(int hp, int spd, int aPwr, int aRt, String alliance)
{
super(hp, spd, aPwr, aRt, alliance);
setImg();
}
/**
* Act - do whatever the Squire wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
update();
}
public void attack()
{
}
public void setImg()
{
if (!withEnemy)
{
for(int i = 1; i < 5; i++)
{
walkImages[i] = new GreenfootImage("minoWalk" + i + ".png");
}
setImage(walkImages[imgNum]);
if(team == "Red")
{
turn(180);
getImage().mirrorVertically();
}
}
else
{
for(int i = 1; i < 4; i++)
{
atkImages[i] = new GreenfootImage("minoAtk" + i + ".png");
}
setImage(atkImages[imgNum]);
if(team == "Red")
{
turn(180);
getImage().mirrorVertically();
}
}
}
public void animate()
{
if (!withEnemy)
{
imgNum = (imgNum + 1) % walkImages.length;
setImage( walkImages[imgNum]);
if(imgNum >= 5)
{
animationComplete = true;
}
}
else
{
imgNum = (imgNum + 1) % atkImages.length;
getImage().scale(500,500);
setImage( atkImages[imgNum]);
if(imgNum >= 5)
{
animationComplete = true;
}
}
}
public void checkForEnemies()
{
if(isTouching(Soldier.class))
{
withEnemy = true;
}
}
public void checkGround()
{
if(animationComplete)
{
remove();
}
}
public void update()
{
if(direction >= 0)
{
setLocation(getX() + speed, getY());
animate();
}
else
{
setLocation(getX() - speed, getY());
animate();
}
}
}
