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

2015/3/12

How can I stop my timer?

Bluefoot98 Bluefoot98

2015/3/12

#
Hello, I´m trying to program a greenfoot game, but I have a problem and I don´t know how to solve it :( My Counter is the Counter which you can download direct in greenfoot (see below). If my actor has 100 points, the game should stop and a message like "Player ! wins the game!" or something like that should appear. Does anybody how I can make it?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael Kölling 
 * @version 1.0
 */
public class Counter1 extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private String prefix;
    
    public Counter1()
    {
        this(new String());
    }

    /**
     * Create a new counter1, initialised to 0.
     */
    public Counter1(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

    /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }

    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
Super_Hippo Super_Hippo

2015/3/12

#
You could add the following at the end of the act method:
if (value>=100)
{
    System.out.println("Player 1 wins the game");
    Greenfoot.stop();
}
danpost danpost

2015/3/12

#
Super_Hippo wrote...
You could add the following at the end of the act method:
if (value>=100)
{
    System.out.println("Player 1 wins the game");
    Greenfoot.stop();
}
True, provided that the only thing a Counter object is created for in the project is the score. If the Counter class is used to create multiple objects for various things (health, lives, acts, etc.), then another way will have to be taken.
Bluefoot98 Bluefoot98

2015/3/12

#
Ah, thanks, it works!
You need to login to post a reply.