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

2016/11/15

Let the actor do something if he is on a specific position

Sk1llG4m3r Sk1llG4m3r

2016/11/15

#
i want if the actor is on a specific Position i want to create a new world
danpost danpost

2016/11/15

#
Add an actor to act as a door or gateway. It can have an image or be transparent. It can have a reference field to hold the world it leads to and check for the actor to contact it and change to that world. Its class code could be like this:
import greenfoot.*;

public class Gateway extends Actor
{
    private World gotoWorld;
    
    public Gateway(World world)
    {
        gotoWorld = world;
        setImage(new GreenfootImge(1, 1)); // to make transparent
    }
    
    public void act()
    {
        if (isTouching(Player.class)) Greenfoot.setWorld(gotoWorld);
    }
}
You can have the world the player is in add a Gateway object when it is created (in its constructor or 'prepare' method). The line would look something like this:
addObject(new Gateway(new Level2()), 590, 380);
You need to login to post a reply.