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

2015/4/6

Need help with ScoreBoard

Cathy3210 Cathy3210

2015/4/6

#
Hi! I'm trying to get my scoreboard to add one everytime Mario collects a coin, but the scoreboard does not increase by one on screen. Here is my scoreboard code:
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class Scoreboard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScoreBoard extends Label
{
    public int score_ = 0;
    public ScoreBoard()
    {
        super("0");
    }

    public int getScore()
    {
        return score_;
    }
       
    /**
     * Act - do whatever the Scoreboard wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        super.act();
        setImage(new GreenfootImage("coins : " + score_, fontSize_, textColor_, backgroundColor_));
    }    
    
    public void addScore()
    {
        score_ ++;
    }
}
Here is the code where Mario collects a coin:
    
    public void collectCoins()
    {
        if(isTouching(Coin.class))
        {
            World w = getWorld();
            Coin coin = (Coin)getOneIntersectingObject(Coin.class);
            
            MarioWorld world = (MarioWorld)w;
            ScoreBoard board = world.getBoard();
            board.addScore();
            
            w.removeObject(coin);
            coinsCollected_ ++; //if Mario touches a coin, his number of coins collected increases by one
            
            Greenfoot.playSound("coin.wav");
       }
    }
    
Lastly, here is my world code:
import greenfoot.*;

/**
 * Write a description of class MarioWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MarioWorld extends World
{
    private GreenfootSound Music = new GreenfootSound("bgmusic.mp3");
    private int spawnTimer_;
    ScoreBoard board = new ScoreBoard();
    /**
     * Constructor for objects of class MarioWorld.
     * 
     */
    public MarioWorld()
    {    
        // Create a new world with 1000x680 cells with a cell size of 1x1 pixels.
        super(1000, 680, 1); 
        
        Mario hero = new Mario();
        addObject(hero, getWidth()/2, getHeight()/2);
        
        Lakitu player = new Lakitu(0.005);
        addObject(player, 26, 35);
        
        ScoreBoard board = new ScoreBoard();
        addObject(board, 876, 36);
        
        Lives lives = new Lives();
        addObject(lives, 92, 647);
        
        spawnTimer_ = 0;
    }

    public ScoreBoard getBoard()
    {
        return board;
    }
    
Any help is appreciated!
danpost danpost

2015/4/6

#
Remove line 29 of the MarioWorld class so that the ScoreBoard object created on line 13 can be put into the world.
Cathy3210 Cathy3210

2015/4/6

#
Thank you very much! It works perfectly now. (:
You need to login to post a reply.