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

2021/11/1

How to make a variable that can access from any class

Gabe1098 Gabe1098

2021/11/1

#
I want to make a score counter but it's annoying that it can only be used in one class. Is there any way you can make a variable that you can like change the variable in one class and detect the variable in another? I tried static, it works, but it doesn't set back when I reset the scenario
Bob2Joe Bob2Joe

2021/11/1

#
make the score variable public and static. For example: public class ScoreClass extends Actor { public static int score = 10; } And then in the class you want to access it, use (The name of the class the score variable is In) in this case ScoreClass. You can use ScoreClass.score to access it from another class. Like So: public class Player extends Actor { public void act() { ScoreClass.score+=1; } }
danpost danpost

2021/11/2

#
Bob2Joe wrote...
<< Code Omitted >>
This will not reset the counter when the scenario is reset. Truthfully, the counter should not be made static as the score in one World object is NOT the score in ALL World objects (even though you will probably only have one world at any one time). Anyway, a simple fix is to assign/initialize the field in the constructor of your World subclass:
public class MyWorld extends World
{
    public static int score;
    
    public MyWorld()
    {
        super(600, 400, 1);
        score = 0; // initializes the score value
        ...
    }
    ...
danpost danpost

2021/11/2

#
As a non-static field:
public class MyWorld extends World
{
    public int score = 0;
    ...
In an Actor subclass (with instance of the class added to the world):
((MyWorld)getWorld()).score += 5;
Gabe1098 Gabe1098

2021/11/2

#
danpost wrote...
As a non-static field:
public class MyWorld extends World
{
    public int score = 0;
    ...
In an Actor subclass (with instance of the class added to the world):
((MyWorld)getWorld()).score += 5;
So, if the variable was made in the snake class, I would do this?
public int score = 0;

public void act() {
    score++;
}
and then I detect it in another class
if (((MyWorld)World())  >60) {
    getWorld().removeObject(this);
}
Is that how I would do that?
danpost danpost

2021/11/3

#
Gabe1098 wrote...
then I detect it in another class
if (((MyWorld)World())  >60) {
    getWorld().removeObject(this);
}
What is the condition you want to apply here? (obviously, line 1 will not work as written)
Gabe1098 Gabe1098

2021/11/3

#
danpost wrote...
Gabe1098 wrote...
then I detect it in another class
if (((MyWorld)World())  >60) {
    getWorld().removeObject(this);
}
What is the condition you want to apply here? (obviously, line 1 will not work as written)
Oh shoot I meant if ((MyWorld)World()).score > 60
danpost danpost

2021/11/3

#
Gabe1098 wrote...
I meant if ((MyWorld)World()).score > 60
You cannot use that if the score is in the Snake class. You must get a reference to the Snake object and use it (instead of a reference to the World object). You may want to keep a reference to the Snake object in your World subclass:
public Snake snake = new Snake();
Then you can use: ((MyWorld)getWorld()).snake.score for the score. Also, this way, the score is not lost upon removing the snake from the world.
You need to login to post a reply.