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

2013/3/24

Need held to end game urgently !

ConorOwens ConorOwens

2013/3/24

#
I want the game to end when the health counter <= 0 ive gotten this far so far but I don't know what to put into the if statement public void EndGame() { if ( totalHealthCount >= 0) { Greenfoot.stop(); Greenfoot.playSound("game-over.wav"); } } heres my health counter code;
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (ConorOwens) 
 * @version (20/02/13)
 */
public class HealthCounter  extends Actor
{
    private int totalHealthCount = 100;

    public HealthCounter()
    {
        setImage(new GreenfootImage("" + totalHealthCount, 60, Color.WHITE, Color.BLACK));
    }
    
    /**
     * Decrease the total amount displayed on the counter, by a given amount.
     */
    public void bumpHealthCount(int amount)
    {
        totalHealthCount += amount;
        setImage(new GreenfootImage("" + totalHealthCount, 60, Color.WHITE, Color.BLACK));
    }
}
Any help would be appreciated
-nic- -nic-

2013/3/24

#
first you should change the
Greenfoot.stop(); 
Greenfoot.playSound("game-over.wav");
around as you are stoping the game before it plays the sound also change >= to <= because you are checking to see if the health is above zero to end the game. so
 if ( totalHealthCount >= 0)
becomes
 if ( totalHealthCount <= 0)
ConorOwens ConorOwens

2013/3/24

#
ive done that but the game still continues on when health counter reaches 0 or below
danpost danpost

2013/3/24

#
Make sure that your 'bumpHealthCount' method is actually decreasing the value of the counter. If you are not supplying a negative number in the parameter, then you need to subtract the amount, not add it. @-nic-, I do not think it matters the order of the two statements. I believe the method will still finish after informing Greenfoot to stop the scenario.
ConorOwens ConorOwens

2013/3/24

#
yeah i have it adding -20 ....... do you have any idea why the game continues after 0 and below ? also the counter starts at 100 and then decreases every time an object hits the player
ConorOwens ConorOwens

2013/3/24

#
I dont think i explained what i need help with clearly, i need the game to end when the health counter is less than or equal to zero.
danpost danpost

2013/3/24

#
I am guessing that the 'EndGame' method is part of the HealthCounter class as well. Where is that method being called from (it should probably be called at line 26 in the code above). I eliminated the extra method and just put the code there, as it will only be called when the count is change anyway. The class should probably look like this:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Counter here.
 * 
 * @author (ConorOwens) 
 * @version (20/02/13)
 */
public class HealthCounter extends Actor
{
    private int totalHealthCount = 100;

    public HealthCounter()
    {
        bumpHealthCount(100);
    }
    
    /**
     * Adjust the total amount displayed on the counter, by a given amount.
     */
    public void bumpHealthCount(int amount)
    {
        totalHealthCount += amount;
        setImage(new GreenfootImage("" + totalHealthCount, 60, Color.WHITE, Color.BLACK));
        if ( totalHealthCount <= 0)
        {
            Greenfoot.playSound("game-over.wav");
            Greenfoot.stop();
        }
    }
}
ConorOwens ConorOwens

2013/3/24

#
thanks alot ! works perfectly really appreciate he help @danpost and @-nic-
-nic- -nic-

2013/3/25

#
its okay
derp2000 derp2000

2013/6/15

#
i've taken this code and put it in my HealthCounter class, but it isn't working yet. |What do I need to put in the other classes to let it work?
danpost danpost

2013/6/15

#
@derp2000, please review this discussion, which is closely related to what you are looking for.
derp2000 derp2000

2013/6/16

#
oke, thanks
You need to login to post a reply.