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

2016/11/24

How to load in multiple worlds through an array

Jillis Jillis

2016/11/24

#
Hi I am currently designing a dungeon crawling game, where there's multiple worlds, each one being a room with enemies/powerups in them. I have designed multiple rooms, where currently walking into a door will trigger the user and door intersecting function and move them into another room, however with this design i have multiple door subclasses spawning the user in different rooms, also I have the worlds spawning the character into a specific place, for example if they walk in through the top of a room, they will spawn on the right due to another door leading on that side. How would I get them to spawn in a different place depending on the door they entered by? I would also like to avoid the multiple door sub classes by storing the whole map as an array for example something like 0,1,2 representing the map I have attached, but I'm just not sure how to do this. All help is appreciated! thanks.. 1,3,4 2,0,0 4,1,5 also if I have explained myself really badly then I apologise, I'll reply with a more specific explanation of what you don't understand.
Jillis Jillis

2016/11/24

#
http://imgur.com/a/JvXoP
Jillis Jillis

2016/11/24

#
012 124 200 415 is what i meant to post for the map, where each number is a design of a world, 0 not being a room and no doors should lead to it
Super_Hippo Super_Hippo

2016/11/24

#
You can pass a parameter to each door object to tell the door where it should lead to:
private int target;

public Door(int targetWorld)
{
    target = targetWorld;
}
Then, when changing the world, use the door's target variable to decide which world to load. Probably you only need one world subclass for all worlds too.
Door door = (Door) getIntersectingObject(Door.class);
if (door != null)
{
    Greenfoot.setWorld(new WorldName(door.target));
}
And finally, in the world subclass:
public WorldName()
{
    super(.....);
    //everything which should be in every room
}

public WorldName(int type)
{
    this();
    switch(type)
    {
        case 1:
        //add objects for room 1
        break;
        
        case 2:
        //add objects for room 2
        break;
        
        //...and so on
    }
}
When adding a door, you pass the target, for example:
addObject(new Door(2), 200, 100);
Jillis Jillis

2016/11/25

#
Thank you!! Wait regarding the first two parts, where are they meant to be in?
danpost danpost

2016/11/25

#
Jillis wrote...
Thank you!! Wait regarding the first two parts, where are they meant to be in?
The first part should be obvious. There is what looks to be a method without a return type. That is a constructor for a Door object; so, the Door class. The second part should be somewhat obvious. It uses 'getOneIntersectingObject' (or what should be that); so, it is in a subclass of Actor (since the method is a member of the Actor class). Also, the intersecting object looked for is a Door object; so, whatever Actor type that intersects a door and causes a change in rooms.
Jillis Jillis

2016/11/29

#
How would I change the background of the world in this way? For example changing it from sand to stone?
Super_Hippo Super_Hippo

2016/11/29

#
If you want every 1 to be sand and every 2 to be stone, then you add that in the switch:
//...
    switch(type)
    {
        case 1:
        setBackground("Sand.png");
        //add objects for room 1
        break;
         
        case 2:
        setBackground("Stone.png");
        //add objects for room 2
        break;
         
        //...and so on
    }
//...
You need to login to post a reply.