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

2015/5/11

Carry score

StuDent463212 StuDent463212

2015/5/11

#
So I've tried to find this before, but had no luck I'm trying to carry the score from my first world, to my second, and then from my second world to my third, then from the third to the endscreen And at the moment I have the basic Score type code: (Score class)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class Score extends Actor
{
    int score = 0;
    final Color TRANSPARENT = new Color(0, 0, 0, 0);

    public void act()
    {
        setImage(new GreenfootImage("Score : " + score, 24, Color.RED, TRANSPARENT));
    }
    
    public void addScore(int amount)
    {
        score = score + amount;
    }
    
    
}
(Grass world class):
public class Desert extends World
{
    Score score = new Score();
}

public Score getScore()
    {
        return score;
    }

Any help would be appreciated
StuDent463212 StuDent463212

2015/5/11

#
Forgot to mention, the Grass world code isn't all of it, but just where the Score class is in the code, the rest isn't related to it.
StuDent463212 StuDent463212

2015/5/11

#
StuDent463212 wrote...
public class Desert extends World
Oh, forgot I copied the wrong world code, meant to be public class Grass extends World
azazeel azazeel

2015/5/11

#
suggestion, use counter from greenfoot import class and make it public static modifier
public class Desert extends World
{
    public static Counter myscore = new Counter("Score : ");
.....
prepare(){
... 
 addObject(myscore , 50,10);
...
}
and when you want to add score just put this code Desert.myscore.add(10);
danpost danpost

2015/5/11

#
Line 3 in you Grass code above declares a local variable 'score'. At the end of the constructor, that reference is no longer valid. Move line 3 to outside the constructor for the 'score' to maintain the reference beyond the execution of the constructor. When changing worlds, you can set the value of the new counter in the new world to that of the old counter in the old world.
StuDent463212 StuDent463212

2015/5/12

#
Okay, so what code would I need to put in the other worlds for the score to carry over. Would it be something like setScore(getValue) or something like that
danpost danpost

2015/5/12

#
StuDent463212 wrote...
Okay, so what code would I need to put in the other worlds for the score to carry over. Would it be something like setScore(getValue) or something like that
It would be something like this:
MyNewWorld mnw = new MyNewWorld();
mnw.getScore().setValue(getScore().getValue());
Greenfoot.setWorld(mnw);
StuDent463212 StuDent463212

2015/5/13

#
I'm getting an error "<Identifier expected>" Between the first 'getScore' and the '()' in the second line
danpost danpost

2015/5/13

#
StuDent463212 wrote...
I'm getting an error "<Identifier expected>" Between the first 'getScore' and the '()' in the second line
Show the code you are using. Cannot fix unseen code. This not only means what you used by my suggestion, but also the 'setScore' method in your world class as well.
You need to login to post a reply.