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:
Here is the code where Mario collects a coin:
Lastly, here is my world code:
Any help is appreciated!
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_ ++;
}
}
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");
}
}
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;
}