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

2021/5/29

Passing Actors Across Words While Keeping Their Varibles

yakub yakub

2021/5/29

#
I am creating a game that has a healthcount, fuelcount and a scorecount. I create a new one of each in Level1, but I want to keep the same old ones when I switch to Level2 and then later to Level3. How can I do that?
Gbasire Gbasire

2021/5/29

#
you can create different ones in each world, but make the variables in healthcount, fuelcount and scorecount static, so that they will stay the same if you create a new object of their class
yakub yakub

2021/5/29

#
Gbasire wrote...
you can create different ones in each world, but make the variables in healthcount, fuelcount and scorecount static, so that they will stay the same if you create a new object of their class
How would I make them static?
Roshan123 Roshan123

2021/5/29

#
static int xyz=927726272762728386362729; Static is used to share one same instance with all other instance of another classes
yakub yakub

2021/5/29

#
But does this work across worlds, or just in one world?
danpost danpost

2021/5/29

#
One needs to be careful with what can or should be static. Counters each should have their own value. Therefore, using static would not be advisable, here. Better would be to pass the actor with those counters from world to world:
public Level2(Spaceship spaceship)
{
    super(??, ??, ??);
    addObject(spaceship, ??, ??);
    addObject(spaceship.healthCounter, ??, ??);
    addObject(spaceship.scoreCounter, ??, ??);
    addObject(spaceship.fuelCounter, ??, ??);
    ...
}
yakub yakub

2021/5/29

#
Thank you again danpost.
You need to login to post a reply.