This site requires JavaScript, please enable it in your browser!
Greenfoot back
ando600
ando600 wrote ...

2015/10/16

Class not acting on input data:

ando600 ando600

2015/10/16

#
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:
ando600 ando600

2015/10/16

#
Sorry, I miss-copy/pasted that second part. The first Set of code is my referee class, and the second part is my world class.
danpost danpost

2015/10/17

#
The main problem is that the bars in your fields are not the bars in your world. You declare and assign bar objects to the fields near the top of the code; but, you create and add different bars into the world near the end of the 'prepare' method. Just remove the lines immediate above the 'addObject' lines for the bars in the 'prepare' method (the lines that create and assign new objects to the variables declared to hold 'BarOne' and 'BarTwo' objects). To be totally clear, the fields near the top and the variables within the method, even though named identically, are not the same things. Once a variable is declared in a method with the same name as a field, the field can then only be reference by placing 'this.' prior to its name; otherwise, it refers to the variable declared within the method.
ando600 ando600

2015/10/17

#
Thank you! Also, your bar class is awesome. Thank you for all of your help to this forum.
You need to login to post a reply.