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

2015/4/8

How to transfer a variable of a object to a new World

ziLaK ziLaK

2015/4/8

#
Hi i have a GameWorld that has a Score count and a Life count. I create a new GameOver world when the life is 0, it is any way to transfer the score to the game over world to say how many points i had? Thx
danpost danpost

2015/4/8

#
ziLaK wrote...
Hi i have a GameWorld that has a Score count and a Life count. I create a new GameOver world when the life is 0, it is any way to transfer the score to the game over world to say how many points i had? Thx
Yes, a couple of basic ways. Pass the score through a parameter in the GameOver world constructor or through a method call after creating the world. Or you could cheat the system and add a 'public static int' field to the GameOver class and set it to the final score before creating the GameOver world.
ziLaK ziLaK

2015/4/8

#
Could you do a example code for the first way you said? "Pass the score through a parameter in the GameOver world constructor" thx again
danpost danpost

2015/4/8

#
In the GameOver world, do the following:
// change this line
public GameOver()
// to this
public GameOver(int finalScore)
and within the code of the constructor (the block we just changed to declaration of), use 'finalScore' for displaying purposes. When creating a GameOver world object, do the following:
// where you have
Greenfoot.setWorld(new GameOver());
// change it to
Greenfoot.setWorld(new GameOver(/** get score to here */));
You need to login to post a reply.