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

2018/10/21

need help with counter

Chipley Chipley

2018/10/21

#
i need help making the game end if my counter reaches 13, here is the counter code if you need it:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.BLACK, new Color(0, 0, 0, 0)));
    }    
    
    public void addScore()
    {
        score++;
    }
}
Super_Hippo Super_Hippo

2018/10/21

#
public class Counter extends Actor
{
    private int score = 0;
    
    public Counter()
    {
        updateImage();
    }
    
    public void updateImage() 
    {
        setImage(new GreenfootImage("Score: " + score, 24, Color.BLACK, new Color(0, 0, 0, 0)));
    }    
     
    public void addScore()
    {
        score++;
        updateImage();
        if (score == 13) Greenfoot.stop();
    }
}
Chipley Chipley

2018/10/22

#
Thanks works great, thank you for the help.
You need to login to post a reply.