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


Variable kept across all classes

mole2003 mole2003


HI all, i am trying to have a score variable on all classes (world and actors) how can I do this
Super_Hippo Super_Hippo


Store the variable in your world and make it possible to access the variable with a getter method.
private int score;

public int getScore() {return score;}
//you might want these too:
public void setScore(int newScore) {score = newScore;}
public void addScore(int amount) {score += amount;}
Then, to access value of the score variable from an object inside this world, you can do:
((MyWorld) getWorld()).getScore(); //replace MyWorld with your world class name
You need to login to post a reply.