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

2013/1/18

Set a world

moobe moobe

2013/1/18

#
Hi, I'd like to set a new level/world. I have an integer called "level". The code Greenfoot.setWorld(level); Will not work, because setWorld expects a world variable, but "level" is an integer. How can this problem be solved?
danpost danpost

2013/1/18

#
Would this be a world with a seperate class altogether, or a re-creation of the same class using the same code with a new level value? My guess is a re-creation, but have to be sure.
moobe moobe

2013/1/18

#
yeah, it is a re-creation. When my actor has died the level shall be recreated :D
danpost danpost

2013/1/18

#
Just like you create an Actor object with: new ActorName() You can create a World object with: new WorldName() The re-created world will need to be told what the value of level is, so it can create the correct set-up. You can create another world constructor in the class to access an int parameter to pass the value:
private int level; // you should already have this

public WorldName()
{
    this(1); // calls the other constructor passing the level value
    // the (1) can be adjusted; like if you start the value at zero
}

// this will be the constructor that does the work
public WorldName(int lev)
{
    super(600, 400, 1);
    level = lev;
    // rest of construction code
Now, when changing levels, you can use
Greenfoot.setWorld(new WorldName(level+1));
You could add more parameters to pass more data (like possibly the current game-score), however, if there are multiple items to pass and methods to call to re-create the world properly then better would be code like the following (without the use of a second constructor):
WorldName world=new WorldName();
world.level=level+1;
world.score=score;
// etc. -- public methods in the re-created world can be called from here also
world.setMainActor(zelda); // this is just an example; the method would have to be created
moobe moobe

2013/1/18

#
That's exactly the thing I needed! But, to be honest, I haven't understood the alternative way. Isn't it easier to expect 2 parameters like this:
public WorldName()  
{  
    this(1, zelda); // calls the other constructor passing the level value  
    // the (1) can be adjusted; like if you start the value at zero  
}  
  
// this will be the constructor that does the work  
public WorldName(int lev, Zelda zelda)  
{  
Cause otherwise I do not know, where to write the code you have written at the bottom of your post. Does it have to be in my actor? I'm confused..
danpost danpost

2013/1/18

#
The second part is coded wherever you create the new world at. Anyway, what you just posted would work fine (as long as 'zelda' is declared and assigned a value among your world instance fields with something like
public Zelda zelda = new Zelda();
Just make sure you set 'this.zelda' to 'zelda' in the working constructor:
public class WorldName(int lev, Zelda zelda)
{
    super(600, 400, 1);
    level = lev;
    this.zelda = zelda; // *****  this line  ****
    // rest of construction
}
moobe moobe

2013/1/18

#
OK, it compiled fine, but there's still a problem... How does my program now, which "new WorldName" it has to use? Cause my Level1 expects the integer parameter "1", so it will only work, when I write:
if (diedInLevel1)
{
Greenfoot.setWorld(new Level1(1, zelda)); 
}
else if (diedInLevel2 .....


That's not the thing I wanted to do :D
moobe moobe

2013/1/18

#
Hmm, I think you haven't understood me: re-creation means repeating the level. I will think about this problem and tell you guys, when I've solved it...
You need to login to post a reply.