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

2011/12/31

Another scoreboard question..

thetibster thetibster

2011/12/31

#
I'm stuck on keeping score in my game... Its Pong, and in the ball class I can easily create a score variable and add to it every time the ball is removed from the game, but what I can't figure out (perhaps just as a result of sleep deprivation) how to access that variable from my scoreboard class. Or is that not the best way to go about it? If someone could please point me in the right direction, that would be lovely :) let me know if you need to see any of my source code.
danpost danpost

2011/12/31

#
In your world class, add the line
public Scoreboard scoreboard = new Scoreboard();
in the variable declaration segment. This puts an object reference in the world. Add to the scoreboard class the following method
public void adjustScore(int scoreToAdjust, int adjustment)
{
    if (scoreToAdjust == 1) score1 += adjustment;
    if (scoreToAdjust == 2) score2 += adjustment;
    updateScoreboard();
}
Then, when someone scores,
((myWorld) getWorld()).scoreboard.adjustScore(whichScore, changeAmount);
thetibster thetibster

2011/12/31

#
Thank you much :) haha
You need to login to post a reply.