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

2020/3/19

new world with array

Starlord_20 Starlord_20

2020/3/19

#
i found a code that used an array for adding actors to the world ( the original discuss) my question: does it somehow work with worlds instead of actors too?
danpost danpost

2020/3/19

#
Starlord_20 wrote...
i found a code that used an array for adding actors to the world (<< Link Omitted >>) my question: does it somehow work with worlds instead of actors too?
You can have an array of World objects. Worlds are not actors and do not "work" similarly to each other. For what reason might you want to use an array of worlds?
Starlord_20 Starlord_20

2020/3/19

#
I have some subclasses of MyWorld called Level_1 , Level_2 ... I wanted to write a method in my Actor that can set the Level:
 void setLevel( int index) {
        World a = new Level_1();
        World b = new Level_2();
        World c = new Level_3();
        World d = new Level_4();
        World e = new Level_5();
        
        World[] world = {a,b,c,d,e};
        Greenfoot.setWorld(world[index]);
    }
that is what I made so far... is there a better way? can you somehow do that with a list so that you don`t need th declare every World?
danpost danpost

2020/3/19

#
Starlord_20 wrote...
I have some subclasses of MyWorld called Level_1 , Level_2 ... I wanted to code a method in my Actor that can set the Level: << Code Omitted >> that is what I made so far... is there a better way?
Better might be the following (so as not to have so many World objects in memory at one time):
void setLevel(int index) {
    World w = null;
    switch (index)
    {
        case 0:  w = new Level_1(); break;
        case 1:  w = new Level_2(); break;
        case 2:  w = new Level_3(); break;
        case 3:  w = new Level_4(); break;
        case 4:  w = new Level_5(); break;
    }
    if (w != null) Greenfoot.setWorld(w);
}
You need to login to post a reply.