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

2013/4/23

Changing Worlds Helps

SWAG SWAG

2013/4/23

#
What would the code be if I wanted to change my world if I'm in front one my actors and press a down key.
Here's some code. I'm assuming you want your player to be in front of a teleporter or something like that:
//In your world
public void teleport()
{
    if (player.canTeleport() && Greenfoot.isKeyDown("down")) //You will need to have an instance variable of the player in the world
        Greenfoot.setWorld(/*World you want to change to*/);
}

//Somewhere in the world constructor
setPaintOrder(Player.class, Teleporter.class);

//In your player
public boolean canTeleport() //Or "canChangeRooms" or whatever you want
{
    return getIntersectingObject(Teleporter.class) != null; //Basically return true if the player is over a teleporter, false if there is nothing there.
}
Hope that helps
edit: "getIntersectingObject" should be "getOneIntersectingObject"
You need to login to post a reply.