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

2014/6/4

How to remember certain int's after new worlds

1
2
3
SAAEngineer SAAEngineer

2014/6/4

#
Is there any way i can remember integers amounts that were used in a world and re-use them in the same world later on after i used Greenfoot.setWorld(new BackgroundLvL1());
dan11 dan11

2014/6/4

#
If you only want to change the background you can use setBackground(), otherwise, you can make a new method inside your World that resets everything except for the integer, and starts up a new level how you want it, that way, you don't have to reset everything and start in a new world.
SAAEngineer SAAEngineer

2014/6/4

#
how do i make this method that you are talking about?
dan11 dan11

2014/6/4

#
What do you want to happen when you start a new level?
danpost danpost

2014/6/5

#
In your BackgroundLvL1 class, do the following
// add instance field
private World previousWorld;
// change constructor declaration line to
public BackgroundLvL1(World world)
// add the following line in the constructor
previousWorld = world;
Then to return to the previous world use
Greenfoot.setWorld(previousWorld);
SAAEngineer SAAEngineer

2014/6/5

#
Cannot find symbol - Variable previousWorld in the class where i say it must return to BackgroundLvL1. :/
davmac davmac

2014/6/5

#
It sounds like you didn't declare the previousWorld instance variable properly (or otherwise, you're trying to set the previous world from a different class). Post the full code for the BackgroundLvL1 class. Also:
where i say it must return to BackgroundLvL1
You originally said you wanted to return to the previous world from BackgroundLvL1. Can you be clear on what you want?
danpost danpost

2014/6/5

#
The code I gave above was intended for the interim world. If you are returning to the previous world from a different class (like an Actor subclass) other than the interim world class, then, if the object that causes the return is only used within the interim world (no instances of that Actor subclass are created in any other world) you can add to that interim world class code the following method:
public World getPreviousWorld()
{
    return previousWorld;
}
and use:
Greenfoot.setWorld(((InterimWorldClassName)getWorld()).getPreviousWorld());
in the Actor subclass. If instances of that class are created from different worlds, then you will need to check to ensure that the world is that interim world before proceeding to change back to the previous world (or make sure that that instance was intended to have the ability to return to the previoius world by checking its field value(s) or state(s)).
SAAEngineer SAAEngineer

2014/6/5

#
@davmac I have a game that works with scorecounter and a roundcounter and i want it to return to the previous state from a different world so that it remembers my score and round i was in, if there is any easy'er way pleas tell me!
SAAEngineer SAAEngineer

2014/6/5

#
@danpost like i wrote to davmac i realized there's possibly an easier way no?
davmac davmac

2014/6/5

#
@davmac I have a game that works with scorecounter and a roundcounter and i want it to return to the previous state from a different world
You still need to explain which world is which - i.e. is BackgroundLvL1 the "previous world" or is it the "different world"?
SAAEngineer SAAEngineer

2014/6/5

#
davmac wrote...
@davmac I have a game that works with scorecounter and a roundcounter and i want it to return to the previous state from a different world
You still need to explain which world is which - i.e. is BackgroundLvL1 the "previous world" or is it the "different world"?
I start in BackgroundLvL1 which is in fact my only level , in this world i display my scorecounter and roundcounter. At the end of the round (BackgroundLvL1) i write Greenfoot.setWorld(new Countdown_3()); and after a couple of worlds (where i dont need the score or roundcounter) i let the world execute Greenfoot.setWorld(new BackgroundLvL1()); and in that world i want to see the scorecounter which has to be the score from the previous BackgroundLvL1. So i wanted to reload my previous BackgroundLvL1 from the last world before a new round has to start. Because i thought if i could reload the previous instance of BackgroundLvL1 the score would still be there. But there's probably an easier way. Thanks in advance.
davmac davmac

2014/6/5

#
Because i thought if i could reload the previous instance of BackgroundLvL1 the score would still be there. But there's probably an easier way.
No, that sounds like the best way. You'll need to pass the BackgroundLvL1 instance to your other worlds; then, when you want to go back to it, don't create a new instance but set the old one as the world instead. See what danpost suggested above, but instead of adding the constructor and variables to the BackgroundLvL1 class, add it to the other world classes (Countdown_3 etc).
SAAEngineer SAAEngineer

2014/6/6

#
davmac wrote...
Because i thought if i could reload the previous instance of BackgroundLvL1 the score would still be there. But there's probably an easier way.
No, that sounds like the best way. You'll need to pass the BackgroundLvL1 instance to your other worlds; then, when you want to go back to it, don't create a new instance but set the old one as the world instead. See what danpost suggested above, but instead of adding the constructor and variables to the BackgroundLvL1 class, add it to the other world classes (Countdown_3 etc).
Do you mean this?
danpost wrote...
The code I gave above was intended for the interim world. If you are returning to the previous world from a different class (like an Actor subclass) other than the interim world class, then, if the object that causes the return is only used within the interim world (no instances of that Actor subclass are created in any other world) you can add to that interim world class code the following method:
public World getPreviousWorld()
{
    return previousWorld;
}
and use:
Greenfoot.setWorld(((InterimWorldClassName)getWorld()).getPreviousWorld());
in the Actor subclass. If instances of that class are created from different worlds, then you will need to check to ensure that the world is that interim world before proceeding to change back to the previous world (or make sure that that instance was intended to have the ability to return to the previoius world by checking its field value(s) or state(s)).
Or this?
danpost wrote...
In your BackgroundLvL1 class, do the following
// add instance field
private World previousWorld;
// change constructor declaration line to
public BackgroundLvL1(World world)
// add the following line in the constructor
previousWorld = world;
Then to return to the previous world use
Greenfoot.setWorld(previousWorld);
davmac davmac

2014/6/6

#
The second one.
There are more replies on the next page.
1
2
3