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

2017/2/10

Scoreboard doesn't keep score

Marrryanx Marrryanx

2017/2/10

#
when the worlds are changing scoreboard is reseted to 0
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class teste here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class teste extends subclass
{   Scor scor = new Scor();
    /**
     * Constructor for objects of class teste.
     * 
     */
    public teste()
    {
        prepare();
    }
    
    private void prepare()
    {
        addObject(scor, 840, 25);
    }
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Scor here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Scor extends raspunsuri
{
    private int puncte;
    /**
     * Act - do whatever the Scor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Scor() 
    {
        setImage(new GreenfootImage("Punctaj : " + puncte, 24, Color.GREEN, Color.BLACK));
    } 

    public void act() 
    {
        addPuncte();
    } 
    
    public void addPuncte()
    { 
        if (Greenfoot.mouseClicked(null))
        {
            Actor clicked = Greenfoot.getMouseInfo().getActor();
            if (clicked != null && clicked instanceof corecte)
            {
                puncte ++;
                setImage(new GreenfootImage("Punctaj : "+ puncte, 24, Color.BLACK, Color.GREEN));
            }
            if (clicked != null && clicked instanceof gresite)
            {
                puncte --;
                setImage(new GreenfootImage("Punctaj : "+ puncte, 24, Color.BLACK, Color.RED));
            }
        }
    }
}
valdes valdes

2017/2/11

#
On line 10 (class Teste) you declare a member variable Scor. It means that object Scor belongs to ONLY that particular object teste. So each world that is a subclass of teste has its own object Scor. So it is not reseting, it is a different object on each world. The solution is to have only one object Scor, common to all the worlds. So you need a CLASS VARIABLE (static). Change line 10 to:
public static Scor scor = new Scor();
You need to login to post a reply.