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

2015/12/2

Dynamic variable for setWorld

Knet Knet

2015/12/2

#
I have one class under World that's called Maps and it's purpose is to set different worlds. I have other Classes that is Map1, Map2 and so on which add objects to the world. I call a method in Maps that's called nextLevel that I want to use to load Map1, Map2 and so on. I do this with: World name = new Map2(); Greenfoot.setWorld(name); The problem here is that I want the variable "name" to be dynamic, I want it so that the first time I run nextLevel it's setWorld(Map2) and the time after it shoud run setWorld(map3) and so on. I want to be able to use a String to define what's the next setWorld is going to be. I have tried to use forName but it seems to only work for Classes and Objects. What's the easiest solution here? Sorry if my explanation is not enough, just ask if something is not clear.
Super_Hippo Super_Hippo

2015/12/2

#
1
2
3
4
5
6
7
public void nextLevel(World w)
{
    if (w instanceof Map1) Greenfoot.setWorld(new Map2);
    else if (w instanceof Map2) Greenfoot.setWorld(new Map3);
    else if (w instanceof Map3) Greenfoot.setWorld(new Map4);
    //...
}
When calling the nextLevel method, you would then pass 'this' from the current world to the Maps class. If you move the nextLevel method to the Map1, Map2... classes, then you could just use one line there. Another way is to set one Map class and create all levels with just this one class. I don't know your game, but it is possible to do this very often. This makes it very easy to create new levels then.
danpost danpost

2015/12/2

#
Maybe you could take some pointers from my Super Level Support Class demo scenario.
Knet Knet

2015/12/3

#
It works great Super_Hippo but I was more looking for a solution that didn't expand as I added more maps. I know it's possible and not hard to do it in one class, I have done it before but it there will be so much code in one class if I add many maps, which I don't like.
1
2
3
4
5
6
7
8
9
10
11
public void nextLevel(World w)
{
    if (w instanceof Map1) Greenfoot.setWorld(new Map2());
    else if (w instanceof Map2) Greenfoot.setWorld(new Map3());
    else if (w instanceof Map3) Greenfoot.setWorld(new Map4());
    //...
 
}
    //in an Actor class
    Maps map = (Maps)getWorld();
    map.nexLevel(getWorld());
So that's the best I have right now but as I said above I'd prefer a solution that doesn't grow as I add more maps.
Super_Hippo Super_Hippo

2015/12/3

#
Knet wrote...
but it there will be so much code in one class if I add many maps, which I don't like.
This doesn't have to be. As I said, I don't know what kind of game you are programming. In my Bomberman game, the levels are all created from one Level class. Each level has an entry in an array. For the first levels, it looks like this:
1
{{3}, {5}, {2,3}, {2,2,2}, {4,2,0}, {6,0}, ...
To add a new level, I only have to add another entry to the array. (If I add a new type of enemy, I have to code that too of course.)
danpost danpost

2015/12/3

#
Knet wrote...
there will be so much code in one class if I add many maps, which I don't like.
With my Level class, each subclass would be able to set its own fields (map data included) and determine which level to proceed to. Since the fields and methods are located in the Level class and methods are overridden in the subclasses, it is not necessary to know what level is currently active (no extensive 'if-else-else-else' statements -- in fact, not even one 'if' after determining that a particular level change is required).
You need to login to post a reply.