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

2013/4/23

HELP

SWAG SWAG

2013/4/23

#
Can someone help me write a code that allows my character to go on top of my teleporter and press the "t" key that will allow him to teleport to the second world.
didn't my previous reply help? http://www.greenfoot.org/topics/3466. Just switch the "down" key to "t"
Another way you could do it is in the Player class instead of the world:
//In the player
public void teleport()
{
    if (!canTeleport())
        return;
    Greenfoot.setWorld(/*The world you want to switch to*/)
}

public boolean canTeleport()
{
    return getIntersectingObject(Teleporter.class) != null;
}
Whoops, edit to canTeleport()
public boolean canTeleport()  
{  
    return getIntersectingObject(Teleporter.class) != null && Greenfoot.isKeyDown("t");
} 
SWAG SWAG

2013/4/23

#
I get error (cannot find symbol - methid getIntersectingObject(java.land.Class etc.......)
getOneIntersectingObject(Teleporter.class) *. My bad. You know all these methods are in the Greenfoot API, right? http://www.greenfoot.org/files/javadoc/
SWAG SWAG

2013/4/23

#
my character is able to go on the top of teleport but wont teleport. This there a code, so the my character knows he is on top of teleporter.
SWAG SWAG

2013/4/23

#
okay it worked thanks
well, you have to tell it to move to a specific world, you could do something like this:
//In your teleporter class
/*The world that I teleport to*/
private World myNewWorld;
...
public Teleporter(World w, /*Anything else*/)
{
    myNewWorld = w;
    ...
}

public void teleport()
{
    Greenfoot.setWorld(myNewWorld);
{
In the player:
//In your player keep your canTeleport method
public void teleport()
{
    if (!canTeleport())
        return;
    Teleport t = getOneIntersectingObject(Teleporter.class);
    t.teleport();
}
The above way would have every Teleporter be a teleporter to a specific world, that way you don't have to make a different Player object for each teleporting world you can go to.
You need to login to post a reply.