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

2013/4/16

Setting the score through different worlds

1
2
danpost danpost

2013/4/17

#
Funny. I look through those blocks of code and seriously did not see that. It might have been because he had called it 'subclasses' instead of 'superclass'. Yes, if the 'WMethod' world extends 'World' and the the other classes that use the score object extend 'WMethod', then they will all share the same field (albeit, with seperate values). The 'setScore' method I gave above should then be placed in the 'WMethod' world code. It might be better to have the 'setScore' method actually add the score object into the world:
public void setScore(Score scoreObj)
{
    theScore = scoreObj;
    addObject(theScore, 100, 640);
}
You can then use this in your initial world in creating and setting up the score object with the following:
Score score = new Score();
setScore(score);
If you continue to get error messages and are not able to correct the code, please post the message you are getting plus the area in the code where the error is occuring.
danpost danpost

2013/4/17

#
You can even create another method for the 'WMethod' class to set the new worlds:
public void setNewWorld(WMethod wm)
{
    wm.setScore(theScore);
    Greenfoot.setWorld(wm);
}
Then all you need for changing worlds is:
if("Espace3".equals(thisW)) ((WMethod)getWorld()).setNewWorld(new Espace3());
welleid welleid

2013/4/18

#
Thanks it works perfectly, I didn't think about setting the method in the subclass, meaning that we keep the same Score in fact.
You need to login to post a reply.
1
2