Hello, I have an actor class called referee, which monitors the game. I am using danpost's Bar class in order to modify player health, and the Referee class accesses that data and decides when the game is over. However, It gives no output (gameOver) when it should. My i know the methods return the correct data, but for some reason my referee cannot act on it.
Here is my referee class:
import greenfoot.*;
import java.awt.Color;
/**
* Write a description of class Referee here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Referee extends Actor
{
public BarOne barone = new BarOne("FighterOne", "Health Points", 100, 100);
public BarTwo bartwo = new BarTwo("FighterTwo", "Health Points", 100, 100);
private int FighterOneDeath, FighterTwoDeath;
/**
* Act - do whatever the Referee wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
FighterOneDeath = barone.getValue();
FighterTwoDeath = bartwo.getValue();
if (FighterOneDeath <= 0)
{
if (getWorld().getObjects(GameOver.class).isEmpty()) showGameOver();
return;
}
if (FighterTwoDeath <= 0)
{
if (getWorld().getObjects(GameOver.class).isEmpty()) showGameOver();
return;
}
}
private void showGameOver()
{
getWorld().addObject(new GameOver(), 600, 225);
}
}
import greenfoot.*;
import java.awt.Color;
/**
* Write a description of class Crossroads here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Crossroads extends World
{ public BarOne barone = new BarOne("FighterOne", "Health Points", 100, 100);
public BarTwo bartwo = new BarTwo("FighterTwo", "Health Points", 100, 100);
private int FighterOneDeath, FighterTwoDeath;
/**
* Constructor for objects of class Crossroads.
*
*/
public Crossroads()
{
super(1200, 400, 1);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
Road road = new Road();
addObject(road, 601, 394);
road.setLocation(601, 394);
FighterOne fighterone = new FighterOne();
addObject(fighterone, 258, 278);
fighterone.setLocation(1085, 290);
fighterone.setLocation(1181, 292);
road.setLocation(867, 147);
removeObject(road);
removeObject(fighterone);
Road road2 = new Road();
addObject(road2, 602, 394);
road2.setLocation(601, 394);
FighterOne fighterone2 = new FighterOne();
addObject(fighterone2, 174, 279);
hitBoxOne hitboxone = new hitBoxOne();
addObject(hitboxone, 95, 281);
FighterTwo fightertwo = new FighterTwo();
addObject(fightertwo, 871, 317);
fightertwo.setLocation(871, 317);
fightertwo.setLocation(1123, 388);
removeObject(fightertwo);
FighterTwo fightertwo2 = new FighterTwo();
addObject(fightertwo2, 921, 301);
hitBoxTwo hitboxtwo = new hitBoxTwo();
addObject(hitboxtwo, 1007, 289);
BarTwo bartwo = new BarTwo("FighterTwo", "Health", 100, 100);
addObject(bartwo, 900, 60);
BarOne barone = new BarOne("FighterOne", "Health", 100, 100);
addObject(barone, 300, 60);
}
public void act()
{
FighterOneDeath = barone.getValue();
FighterTwoDeath = bartwo.getValue();
if (FighterOneDeath <= 0)
{
if (getObjects(GameOver.class).isEmpty()) showGameOver();
return;
}
if (FighterTwoDeath <= 0)
{
if (getObjects(GameOver.class).isEmpty()) showGameOver();
return;
}
}
private void showGameOver()
{
addObject(new GameOver(), getWidth() / 2, getHeight() / 2);
}
}
I have also tried to run it via the world, and it still does not work. I thought that declaring instance variables would work in the world, but it did not make a difference. Here is my world class:
