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

2014/12/16

Need Help with adding a 'health pack' to my game!

1
2
JLItachi JLItachi

2014/12/16

#
Hi, I am currently making a game right now which involves animals eating each other. I am quite new to Greenfoot. So I have added a health bar which has 5 lives. I want to add a health pack which my animal can go 'touch' and it will give them 1 extra life (Give back one of the lives). Help would be appreciated. This is the code for the healthbar. public HealthBar() { Check(); } public void act() { Check(); } public void Check() { setImage(new GreenfootImage(healthBarWidth +2, healthBarHeight + 2)); GreenfootImage myImage = getImage(); myImage.setColor(Color.BLACK); myImage.drawRect(0, 0, healthBarWidth + 1, healthBarHeight + 1); myImage.setColor(Color.BLUE); myImage.fillRect(1, 1, health*pixelsperHealthPoint, healthBarHeight); } public void loseHealth() { health--; }
JLItachi JLItachi

2014/12/16

#
This is the code for the actor which is making the animal lose health by 'hurting' it. If you could reply if the code you need I will be happy to post it. public void act() { MoveRandom(); turnAround(); hurtTurtle(); } public void hurtTurtle() { Actor turtle = getOneIntersectingObject(Turtle.class); if(turtle != null) { //World myWorld = getWorld(); PondWorld pond = (PondWorld)getWorld(); HealthBar healthbar = pond.getHealthBar(); if (touchingTurtle == false) { healthbar.loseHealth(); Greenfoot.playSound("ouch.wav"); replaceEagle(); touchingTurtle = true; if(healthbar.health<0) { eatFood(Turtle.class); Greenfoot.playSound("gameOver.wav"); GameOver gameover = new GameOver(); pond.addObject(gameover, pond.getWidth()/2, pond.getHeight()/2); Greenfoot.stop(); } } } else { touchingTurtle = false; } } private void replaceEagle() { Eagle newEagle= new Eagle(); World pWorld = getWorld(); int x=Greenfoot.getRandomNumber(pWorld.getWidth()); int y=Greenfoot.getRandomNumber(pWorld.getHeight()); pWorld.addObject(newEagle, x, y); }
danpost danpost

2014/12/16

#
At the moment (as far as I know), the only method in the HealtbBar class that can be called to change the value of the 'health' field in the class is the 'loseHealth' method which only decreases its value. You will need to either adjust this method so that it does more than just decrease the value or add another method that can increase its value. Then you can call it when the actor eats a health pack.
JLItachi JLItachi

2014/12/16

#
So you mean that I will have to add a method like this: public void gainHealth() { health--; } And then I should put in my turtle code (the actor which is going to 'eat' the health pack) public void eatHealthPack() { if(isFood(HealthPack.class)) { eatfood(HealthPack.class); And in the HealthPack code gainHealth(1). Is this right or do I have to do something else?
danpost danpost

2014/12/16

#
Sort of. For one thing, it should not be 'health--;' in the 'gainHealth' method; it should be 'health++;'. For another, you will probably need these lines in the turtle code before calling 'healthbar.gainHealth();':
1
2
PondWorld pond = (PondWorld)getWorld();
HealthBar healthbar = pond.getHealthBar();
Please note that as you have coded the 'gainHealth' method, it is not 'healthbar.gainHealth(1);', but just 'healthbar.gainHealth();'.
JLItachi JLItachi

2015/1/3

#
Hi, sorry for getting back to you so late. Here is what I have done so far. In the HealthPack I have added the following code into it. public void act() { Healthbar.gainHealth(1); } public void gainHealth() { health++; } And this is what I have put into the turtle code: public void eatHealthPack() { if(isFood(HealthPack.class)) { eatFood(HealthPack.class); PondWorld pond = (PondWorld)getWorld(); HealthBar healthbar = pond.getHealthBar(); Healthbar.gainHealth(); } } It is giving me the following error message which I do not know how to fix: "cannot find symbol- variable Healthbar". Thanks again for your help so far.
JLItachi JLItachi

2015/1/3

#
Also, after the turtle dies, I have made it so it makes the game stops and display GameOver at the end of the screen. Is there any way I will be able to display the final end score along with it? I have fixed the previous error I was getting but now I am getting this error "non-static method gainHealth() cannot be referenced from a static context".
danpost danpost

2015/1/3

#
First, remove the act method from the HealthPack class. I do not think you want the value of health to increment every act cycle. Next, with the last two lines of your Turtle code, the first gets a reference to the healthbar in a variable called 'healthbar' (starting with a lowercase letter -- which is by convention the right way to name a variable). But, in the following line you use 'Healthbar', which is neither the class name nor the variable name (the class name is 'HealthBar', with an uppercase 'H' and 'B'; and the variable name has no uppercase characters). You must be consistent with how you name your class names and variables.
JLItachi JLItachi

2015/1/4

#
Okay, thanks for the clarification. Now, as I have previously mentioned I am getting the following error message when I try to compile my game "non-static method gainHealth() cannot be referenced from a static context". How am I able to fix this error message?
danpost danpost

2015/1/4

#
Just do not call methods on the class when you want a specific object of the class to do what the method does. For example, you do not want the class to game a health point, you want the object of the class to gain a health point.
1
2
3
4
5
// not this:
Healthbar.gainHealth();
// this:
Healthbar healthbar = pond.getHealthBar();
healthbar.gainHealth();
The class does not have a 'health' value; the objects created from the class do. The class is just blueprint for the objects it creates.
JLItachi JLItachi

2015/1/4

#
Okay, now I am getting a different error message every time to change it to what you are saying. I am going to post all the relevant code here so you are able to see what I am doing wrong because I have no idea why it isn't working. The following code is what I have in the HEALTH PACK: public HealthPack() { HealthBar.gainHealth(1); } public void gainHealth() { health++; } This is the code for the turtle eating the healthpack: public void eatHealthPack() { if(isFood(HealthPack.class)) { eatFood(HealthPack.class); PondWorld pond = (PondWorld)getWorld(); HealthBar healthbar = pond.getHealthBar(); healthbar.gainHealth(); } } This is the code for the healthbar: public void gainHealth() { health++; }
danpost danpost

2015/1/4

#
Ok. The turtle is supposed to gain health when it eats a health pack. The health pack itself should not have a health value. Or, if it does, I would think it would remain a constant value -- that is, the amount of health provided to the turtle can be held within the health pack so that different packs can give different increases in the value of the turtle's health. But, from what I can tell, all health packs should increase the value of the health of the turtle by one. The health pack therefore does not even need a health value; it just needs to exist and be eaten. The following can be the entire HealthPack class code:
1
public class HealthPack extends greenfoot.Actor {}
Replace your entire HealthPack class code with that one line and then run the scenario. Report back any errors you may then be getting. It is important to state what the message says exactly and provide the code around which the error points. Also, use the 'code' link below the reply box to insert code into your posts.
JLItachi JLItachi

2015/1/4

#
Okay, I have done what you said and replaced my entire HealthPack code. Now I am getting the follow error message "class HealthPack is already defined in package unnamed package". The code which is highlighted is "class HealthPack extends greenfoot.Actor {}"
danpost danpost

2015/1/4

#
Maybe you misunderstood what had to be done. That one line is to replace EVERYTHING in your HealthPack class. Open the editor on the class and replace everything in it with that one line. Undo any previously changes you have made regarding that line. BTW, if you had the HealthPack class extending something other than Actor, then replace 'greenfoot.Actor' in the line with what you had it extending.
JLItachi JLItachi

2015/1/4

#
Okay, now my game is finally able to run but I have run into another problem. When this problem is solved I will be finally done (and stop annoying you). So the problem is that the HealthPack is not being eaten by the turtle. I do not know why it is doing this.
There are more replies on the next page.
1
2