Hi! I'm pretty new to Greenfoot and I'm making a game where you fight a boss that has different attack cycles that are random. When the boss is in an attack cycle, I have a boolean called "inAttack" that is set to true and then set to false when the cycle is over to prevent any other things from happening in my Boss class while it is in one of its attack cycles. My different attack cycles are called by a random number.
My problem is that when my "inAttack" boolean is set to true in my Attack1() method, it instantly becomes set to false, and my random number starts my Attack2() method. How do I make the boolean not become set to false again? Here is my code in my Boss class:
public class Boss extends Actor
{
private GreenfootImage dI1 = new GreenfootImage("bossI1.png");
private GreenfootImage dI2 = new GreenfootImage("bossI2.png");
private GreenfootImage dI3 = new GreenfootImage("bossI3.png");
private GreenfootImage dR1 = new GreenfootImage("bossR1.png");
private GreenfootImage dR2 = new GreenfootImage("bossR2.png");
private GreenfootImage dR3 = new GreenfootImage("bossR3.png");
private GreenfootImage dL1 = new GreenfootImage("bossL1.png");
private GreenfootImage dL2 = new GreenfootImage("bossL2.png");
private GreenfootImage dL3 = new GreenfootImage("bossL3.png");
private int timeI = 20;
private int timeR = 7;
private int timeL = 7;
public int hSpeed = 0;
public int vSpeed = 0;
private int random = 1;
private int trans = 255;
private int hearts = 20;
private int hitCooldown = 25;
private int endTrans = 255;
private boolean ifStartMove = false;
private boolean inAttack = false;
private boolean shieldUp = false;
private boolean x1 = false;
private boolean y1 = false;
private boolean d1 = false;
private boolean e1 = false;
private boolean z1 = false;
private boolean f1 = false;
private int time1 = 100;
private boolean x2 = false;
private boolean y2 = false;
private boolean d2 = false;
private boolean e2 = false;
private int time2 = 500;
public Boss()
{
dI1.scale(dI1.getWidth()*6, dI1.getHeight()*6);
dI2.scale(dI2.getWidth()*6, dI2.getHeight()*6);
dI3.scale(dI3.getWidth()*6, dI3.getHeight()*6);
dR1.scale(dR1.getWidth()*6, dR1.getHeight()*6);
dR2.scale(dR2.getWidth()*6, dR2.getHeight()*6);
dR3.scale(dR3.getWidth()*6, dR3.getHeight()*6);
dL1.scale(dL1.getWidth()*6, dL1.getHeight()*6);
dL2.scale(dL2.getWidth()*6, dL2.getHeight()*6);
dL3.scale(dL3.getWidth()*6, dL3.getHeight()*6);
setImage(dI1);
}
/**
* Act - do whatever the Boss wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setLocation(getX()+hSpeed, getY());
setLocation(getX(), getY()+vSpeed);
animation();
startMove();
cycle();
shield();
healthBar();
attacked();
die();
attack1();
attack2();
}
public void die()
{
if (hearts < 1)
{
hSpeed = 0;
hearts = 0;
setRotation(90);
inAttack = true;
random = 0;
setImage(dI1);
dI1.setTransparency(endTrans);
if (!isTouching(Platform.class))
{
vSpeed = 1;
dI1.setTransparency(255);
}
if (isTouching(Platform.class))
{
vSpeed = 0;
if (endTrans > 0)
{
endTrans = endTrans - 1;
}
if (endTrans == 0)
{
getWorld().removeObject(this);
}
}
}
}
public void shield()
{
if (shieldUp == true && ifShielded() == false)
{
Shield shield = new Shield();
getWorld().addObject(shield, getX(), getY());
}
if (shieldUp == false)
{
removeTouching(Shield.class);
}
}
public void healthBar()
{
getWorld().showText("Boss Health: " + hearts, 250, 30);
}
public int hReturn()
{
return hSpeed;
}
public int vReturn()
{
return vSpeed;
}
public boolean ifShielded()
{
if (isTouching(Shield.class))
{
return true;
}
else
{
return false;
}
}
public void attacked()
{
if (hitCooldown > 0)
{
hitCooldown = hitCooldown - 1;
}
if (isTouching(BSlash.class) && ifShielded() == false && hitCooldown == 0)
{
hearts = hearts - 1;
hitCooldown = 25;
}
}
public void cycle()
{
if (inAttack == false && ifStartMove == true)
{
random = Greenfoot.getRandomNumber(2)+1;
}
}
public void attack2()
{
if (random == 2)
{
inAttack = true;
if (x2 == false)
{
if (getX() > 100)
{
hSpeed = -2;
animateLeft();
}
else
{
animateIdle();
}
if (getY() > 100)
{
vSpeed = -1;
}
if (getX() == 100)
{
hSpeed = 0;
}
if (getY() == 100)
{
vSpeed = 0;
}
}
if (getX() == 100 && getY() == 100)
{
x2 = true;
shieldUp = true;
if (time2 > -30)
{
time2 = time2 - 1;
}
if (time2 == 450 || time2 == 400 || time2 == 350 || time2 == 300 || time2 == 250 || time2 == 200 || time2 == 150 || time2 == 100 || time2 == 50 || time2 == 0 || time2 == 475 || time2 == 425 || time2 == 375 || time2 == 325 || time2 == 275 || time2 == 225 || time2 == 175 || time2 == 125 || time2 == 75 || time2 == 25)
{
Fireball fireball = new Fireball();
getWorld().addObject(fireball, getX(), getY()+12);
}
}
else
{
shieldUp = false;
}
if (time2 < -26)
{
if (getX() < 299)
{
hSpeed = 2;
animateRight();
}
else
{
animateIdle();
e2 = true;
}
if (getY() < 200)
{
vSpeed = 1;
}
else
{
d2 = true;
}
if (e2 == true && d2 == true)
{
setLocation(300, 200);
y2 = true;
}
}
if (getX() == 300 && getY() == 200 && y2 == true)
{
animateIdle();
hSpeed = 0;
vSpeed = 0;
inAttack = false;
x2 = true;
}
}
if (random != 2)
{
x2 = false;
y2 = false;
e2 = false;
time2 = 500;
}
}
public void attack1()
{
if (random == 1 && ifStartMove == true)
{
inAttack = true;
if (getY() != 1 && x1 == false && f1 == false)
{
vSpeed = -1;
animateIdle();
}
else if (getY() == 1 && f1 == false)
{
vSpeed = 0;
setImage(dI2);
f1 = true;
}
if (f1 == true && x1 == false)
{
dI2.setTransparency(trans);
if (trans > 0)
{
trans = trans - 5;
}
if (trans == 0)
{
x1 = true;
}
}
if (x1 == true)
{
AttackBoss attackboss = new AttackBoss();
getWorld().addObject(attackboss, 300, 1);
x1 = false;
y1 = true;
}
if (y1 == true)
{
if (time1 > 0)
{
time1 = time1 - 1;
}
if (time1 < 1)
{
setLocation(300, 200);
y1 = false;
e1 = true;
}
}
if (e1 = true)
{
animateIdle();
getImage().setTransparency(trans);
if (trans < 251)
{
trans = trans + 5;
}
if (trans == 255)
{
z1 = true;
e1 = false;
}
}
if (z1 == true)
{
dI1.setTransparency(255);
dI2.setTransparency(255);
dI3.setTransparency(255);
inAttack = false;
ifStartMove = true;
z1 = false;
}
}
if (random != 1)
{
e1 = false;
d1 = false;
x1 = false;
y1 = false;
z1 = false;
f1 = false;
}
}
public void startMove()
{
if (getX() < 300 && ifStartMove == false)
{
hSpeed = 1;
}
else if (ifStartMove == false)
{
hSpeed = 0;
}
if (getX() > 299 && ifStartMove == false)
{
ifStartMove = true;
}
}
public void animation()
{
if (hSpeed < 0 && inAttack == false)
{
animateLeft();
}
if (hSpeed > 0 && inAttack == false)
{
animateRight();
}
if (hSpeed == 0 && inAttack == false)
{
animateIdle();
}
}
public void animateIdle() //running right animation
{
timeI = timeI - 1;
if (timeI == 0)
{
timeI = 20;
}
if (getImage() == dI3 && timeI == 1 || getImage() == dL1 || getImage() == dL2 || getImage() == dL3 || getImage() == dR1 || getImage() == dR2 || getImage() == dR3)
{
setImage(dI1);
}
else if (getImage() == dI1 && timeI == 1)
{
setImage(dI2);
}
else if (getImage() == dI2 && timeI == 1)
{
setImage(dI3);
}
}
public void animateRight() //running right animation
{
timeR = timeR - 1;
if (timeR == 0)
{
timeR = 7;
}
if (getImage() == dR3 && timeR == 7 || getImage() == dL1 || getImage() == dL2 || getImage() == dL3 || getImage() == dI1 || getImage() == dI2 || getImage() == dI3)
{
setImage(dR1);
}
else if (getImage() == dR1 && timeR == 7)
{
setImage(dR2);
}
else if (getImage() == dR2 && timeR == 7)
{
setImage(dR3);
}
}
public void animateLeft() //runnin left animation
{
timeL = timeL - 1;
if (timeL == 0)
{
timeL = 7;
}
if (getImage() == dL3 && timeL == 7 || getImage() == dR1 || getImage() == dR2 || getImage() == dR3 || getImage() == dI1 || getImage() == dI2 || getImage() == dI3)
{
setImage(dL1);
}
else if (getImage() == dL1 && timeL == 7)
{
setImage(dL2);
}
else if (getImage() == dL2 && timeL == 7)
{
setImage(dL3);
}
}
}