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

2014/10/24

Side Scroller, room to room

Winds Winds

2014/10/24

#
I'm pretty new to Greenfoot, just started about a couple weeks ago. So I've been working on a sidescroller game and I've run into something I'm not sure about. When the player goes past a certain x coordinate, the world will change to the next room. I want to make it so when the player goes below 0 on the x coordinate, the world will change to the previous room. However, when I do so, a new world is created and the player is created on the left side of the screen instead of the right side of the screen. How can I make it so the player starts on the left side of the screen when I go backwards to a previous room?
Super_Hippo Super_Hippo

2014/10/25

#
You could pass the current coordinates of the player when he reaches the edge. So something like this:
private int currentRoom;
public Player(int room)
{
    currentRoom = room;
}

public void act()
{
    //...
    if (getX() < 1 || getX() > getWorld().getWidth()-2 )
    {
        switch(currentRoom) //or however you do it. Just pass the 'getX()' and 'getY()' then. Maybe you can also use one world for all rooms and only have a variable saying which room it is.
        {
            case 2: Greenfoot.setWorld(new Room1(getX(), getY())); break;
            case 3: Greenfoot.setWorld(new Room2(getX(), getY())); break;
            //...
        }
    }
}
public class Room1 extends Room //at least if the rooms are similar. If not, just use 'extends World' and change the 'super()' part
public Room1(int x, int y)
{
    super();
    addObject(new Player(1), getWidth()-x, y);
}
As an alternative, you can just pass a boolean saying if you want to go to the next or previous room and then place it on the left or right side.
Winds Winds

2014/10/25

#
Thank you so much! Out of curiosity, is there any way you can say, if the player is coming from room 3, then in room 2, the player will spawn on the left side?
You need to login to post a reply.