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.
//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; }
public boolean canTeleport() { return getIntersectingObject(Teleporter.class) != null && Greenfoot.isKeyDown("t"); }
//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 your player keep your canTeleport method public void teleport() { if (!canTeleport()) return; Teleport t = getOneIntersectingObject(Teleporter.class); t.teleport(); }