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

2011/9/17

Failing at scoring

rvevea rvevea

2011/9/17

#
Okay I've got another one. I want to setup a scoring system and I feel like I'm pretty damn close ... but just can't seem to figure something out. A brief overview: I've got a super class with a check and set score method:
public int Gscore = 0;

public void act() {
public void setScore(int score) {
        Gscore = score;
    }
    
    public int getScore() {
        return Gscore;
    }
}
From here we move to my player class which is eating things, increasing a score variable, and then calling the setScore() method and passing the score within it:
score = score + 1;
setScore(score);
This all seems to work correctly, here comes the part that I believe is broken. I now move to my score class where I call getScore() which should return whatever the score via Gscore. Problem is, it doesn't seem like it does at all. Here's what 've got in my score class essentially:
int score = getScore();

        if (score == 1) {
            setImage("images/1.png");
        }
everything compiles great, and my brain says this should work. Someone show me the error of my ways! Much appreciated :)
nccb nccb

2011/9/17

#
For the most part it looks like it should work. The first sample seems odd: your getScore() and setScore() are inside act(), which won't compile -- since you say it compiles, I presume that's a copy-and-paste problem. I can't see an obvious flaw (though in the second snippet, how do you initialise score?). As a next step, you could upload your scenario with source code so that we can take a look. Otherwise, you could call getScore() on your actor by pausing the scenario and using the right-click menu, to find out what the score is. One bug could be that score is being incremented twice, and is now 2 rather than 1. Another bug could be that you're asking a different actor for its score than the actor where the score increased. It's hard to say without knowing more.
rvevea rvevea

2011/9/18

#
Thanks for the reply! Yea, I sorta screwed up my copy/pasting as i was in a bit of a hurry. Still really confused as to why I can't get it to work. I went ahead and uploaded an updated version here: http://greenfootgallery.org/scenarios/3463 Basically what i've been able to determine via a System.out.println test is that: The score is being set correctly from: Durptheunicorn > Actions.setScore() > but from getScore() to > Score class there is something going wrong Thanks again for the reply and maybe if you see something weird let me know? Would be very much appreciated :)
nccb nccb

2011/9/19

#
Hi, I know some of this is due to fiddling when it didn't work, but you currently have three different scores being kept in your program, and use a different one in different places. You have Gscore in the Actions class, with getScore() and setScore(). Since Durptheunicorn and Score both inherit from Actions, they both get separate instances of Gscore. When Durp calls setScore(), he's setting his own score, which is separate from the one that Score gets when it calls getScore(). To complicate things further, Durp also has a variable called score, which is being modified. Here's what I would do, based on the current design. Remove the Gscore, getScore() and setScore() from Actions. Keep the score variable in Durp, and add a getScore() method. The final piece of wiring you'll need is to make Score know about Durp, so it can ask Durp for its score. There's some details in this article, but tailoring it to you, you'll need to add this in the Score class:
private Durptheunicorn durp;

public Score(Durptheunicorn durp)
{
    this.durp = durp;
}
Then change Worldofdurp so that you make Durp before score, and keep a reference:
Durptheunicorn durp = new Durptheunicorn();
addObject(durp, 100, 200);
addObject(new Score(durp), 50, 50);
Then in the Score class, when you want to display the score, you use:
int score = durp.getScore();
rvevea rvevea

2011/9/19

#
Thank you for your extremely helpful replies! Got it working and am pretty excited! Very very much appreciated Nccb!
danpost danpost

2011/9/20

#
NVM
You need to login to post a reply.