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

2013/1/10

How to end the game if there are no lives left

LonelyCreeper LonelyCreeper

2013/1/10

#
In my game the player has a certain amount of lives that are recorded using a counter. How do I make the animation stop when the life count reaches 0?
danpost danpost

2013/1/10

#
In the code where the player looses a life and right after where you decrease the counter, add the following line:
if (counter.getValue()==0) Greenfoot.stop();
The 'counter.getValue()' part is just getting the number of lives remaining and needs to be adjusted for your code.
danpost danpost

2013/1/10

#
This post has a simple Counter class code listing that should suit your needs. Again if it is the only one counter in your world and you do not have a reference to it, you can get its value with:
int value = ((Counter)getWorld().getObjects(Counter.class).get(0)).getValue();
LonelyCreeper LonelyCreeper

2013/1/11

#
it says that non-static method getValue() cannot be referenced from a static context. what does that mean?
danpost danpost

2013/1/11

#
I need to know the following: (1) whether you are using the 'simple counter' class that I suggested, or not (and if not, show your counter class) (2) how you are calling 'getValue' (show the whole method that the error occurs)
LonelyCreeper LonelyCreeper

2013/1/11

#
this is the code for the life counter:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Color;
import java.awt.Graphics;

/**
 *LifeCounter that displays a number with a text prefix.
 * 
 * @author Michael Kolling
 * @version 1.0.2
 */
public class LifeCounter extends Actor
{
    private static final Color textColor = new Color(0, 0, 0);
    
    private int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;

    public LifeCounter()
    {
        this("");
    }

    public LifeCounter(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(textColor);
        updateImage();
    }
    
    public LifeCounter(String prefix, int lives)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;
        target = lives;
        value = lives;
        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(textColor);
        updateImage();
    }
    
    public void act() 
    {
        if(value < target) {
            value++;
            updateImage();
        }
        else if(value > target) {
            value--;
            updateImage();
        }
    }

    public void add(int score)
    {
        target += score;
    }

    public void subtract(int score)
    {
        target -= score;
    }

    public int getValue()
    {
        return value;
    }
    
    public int getTarget()
    {
        return target;
    }

    /**
     * Make the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 12);
    }
}
this is the code calling the method:
public void lookForCrab()
{
        if ( canSee(Crab.class) ) 
        {
            eat(Crab.class);
            changeImage();
            crabLives.subtract(1);
            Greenfoot.playSound("au.wav");
            if (!getWorld().getObjects(LifeCounter.class).isEmpty())
        {
            ((LifeCounter) getWorld().getObjects(LifeCounter.class).get(0)).subtract(1);
            if (LifeCounter.getValue()==0) 
            {Greenfoot.stop();}
        }
        }
}
vonmeth vonmeth

2013/1/11

#
Notice the way you subtracted 1 from the lifecounter? You need to call the getValue method in the same way.
danpost danpost

2013/1/11

#
Change the calling method to the following:
public void lookForCrab()
{
    if (canSee(Crab.class))
    {
        eat(Crab.class);
        changeImage();
        crabLives.subtract(1);
        Greenfoot.playSound("au.wav");
        // only the following was changed
        if (crabLives.getValue()==0) Greenfoot.stop();
    }
}
You need to login to post a reply.