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

2014/9/4

Calling score value

Spock Spock

2014/9/4

#
In my game im trying to grab the value of the current score value from the class score to generate a new level. I keep getting the error, "non-static method getValue() cannot be referenced from a static context". This occurs on line 5. I know it's probrably a really easy fix, but I've forgotten how to do it :( Code from world class (SSGame):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void showScore(int delay)
{
    if (currentlevel <= 3)
    {
        addObject(new Message("You've stopped them... for now.\nScore:" + Score.getValue()), getWidth()/2, getHeight()/2);
        Greenfoot.delay(delay);
        addObject(new Message("Get ready for level " + currentlevel + "!"), getWidth()/2, getHeight()/2);
        Greenfoot.delay(delay);
        Music.stop();
        Greenfoot.setWorld(new SSGame(currentlevel, Score.getValue()));          
    }
    else
    {
        addObject(new Message("You stopped the alien invation! \nReturning you back to the main menu..\n Try out another game!\nFinal Score: " +score.getValue()), getWidth()/2, getHeight()/2);
        Music.stop();
        Win.play();
        Greenfoot.delay(delay*2);
        Greenfoot.setWorld(new Menu());        
    }
}
Code from Score class:
1
2
3
4
public int getValue()
{
    return value;
}
Super_Hippo Super_Hippo

2014/9/4

#
The error occurs because the method 'getValue' is not static. That means, you can not call it without a reference to the actual object. 'Score.getValue()' wants to call the method in the class. Ways to solve it: 1. have 'value' in the 'Score' class public and static and use 'Score.value' instead of the getter-method. 2. have 'value' in the 'Score' class private and static, have the method 'getValue' also static. 3. get a reference to the actual object of the 'Score' class and call the method on this object. Probably there are even more ways to fix this. Use (3), if you don't have only one object of the 'Score' class. Be aware that you have to initialize static fields. If you don't do it, the score won't be reset if you reset the scenario.
Spock Spock

2014/9/5

#
Thanks mate
Spock Spock

2014/9/5

#
Also i have another problem. I have no idea why, it was working before. In my world class it comes up with the error "cannot find symbol - variable score". Code:
1
2
3
4
public Score getScore()
{
    return score;
}
danpost danpost

2014/9/5

#
It is difficult to say what is wrong, as you have neither shown what modification you have made within the Score class nor, and more importantly, how the 'score' field is declared in your world subclass. However, I believe your original problem was that you already had a reference to the Score object in a 'score' field; but, in line 5 and in line 10, you tried to used the name of the class, 'Score', instead of the name of the object, 'score' to extract 'value' from. I think you could have solved the original problem just by changing 'Score.getValue()' to 'score.getValue()' in those lines.
Spock Spock

2014/9/5

#
Sorry Dan, I forgot to add that. I modified the score and world class using method 2 of Hippos post. I do not have a constructor of the score field in the world subclass. I have already created a score object in another world method, is there a way to reference this object already made? Relevent world code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void CreateWorld()
{
    //adding area where player will type
    PType ptype = new PType();
    addObject(ptype, 450, 565);
    //adding shield
    Shield shield = new Shield();
    addObject(shield, 450, 510);
    //adding score and health
    Score score = new Score(0);
    addObject(score, 765, 575);
    addObject(health, 148, 575);
}
public Score getScore()
{
    return score;
}
danpost danpost

2014/9/5

#
As written the variable 'score' declared on line 10 is only local to the method; that is, as soon as the method is done executing, that variable is done with. To declare a variable that will last as long as the object that contains it (the world object, in this case), you need to declare an instance field (a variable that belongs to the world instance). This is done by declaring it outside the local blocks of the methods (or constructors) -- just like you have done with your 'health' field (I presume).
Spock Spock

2014/9/7

#
Thanks for that :)
You need to login to post a reply.