Is it possible for me to have 1 class have a different function for each world it is in? For example, when I start making Turtle Adventure 2, I want to have less classes in the game so I can have more stuff.


if (world instanceof WorldOne) { ... } if(world instanceof WorldTwo) { ... } // etc.
import greenfoot.*; public class Morph extends Actor { private int speed; public Morph() { } public void addedToWorld(World world) { if (world instanceof Land) { setImage("turtle.png"); speed=1; } if (world instanceof Air) { setImage("bird.png"); speed=5; } if (world instanceof Sea) { setImage("fish.png"); speed=3; } } public void act() { if (world instanceof Land) { // actions for turtle here } // etc. } // any other methods for this class }
if (speed>0) speed--; // slow the ball down if (!getNeighbours(getImage().getWidth(), true, Ball.class).isEmpty()) collide();
public void addedToWorld(World world) { if (world instanceof Boss2) { if (getY()<370) setLocation(getY(), getX() + 1); //if (getX()<370) setLocation(getX()+1, getY()); } } /** * Act - do whatever the SpaceTurtle2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (world instanceof Boss2) { if (getY()<370) setLocation(getY(), getX() + 1); //if (getX()<370) setLocation(getX()+1, getY()); } move(3); checkKeys(); ifCanSeeArrow(); ifCanSeeFactory(); }
public void addedToWorld(World world) { if (world instanceof Boss2) { if (getY()<370) setLocation(getY(), getX() + 1); //if (getX()<370) setLocation(getX()+1, getY()); } } /** * Act - do whatever the SpaceTurtle2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (world instanceof Boss2) { if (getY()<370) setLocation(getY(), getX() + 1); //if (getX()<370) setLocation(getX()+1, getY()); } move(3); checkKeys(); ifCanSeeArrow(); ifCanSeeFactory(); }
public void addedToWorld(World world) { if (world instanceof Boss2) { if (getY()<370) setLocation(getY(), getX() + 1); //if (getX()<370) setLocation(getX()+1, getY()); } } /** * Act - do whatever the SpaceTurtle2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (world instanceof Boss2) { if (getY()<370) setLocation(getY(), getX() + 1); //if (getX()<370) setLocation(getX()+1, getY()); } move(3); checkKeys(); ifCanSeeArrow(); ifCanSeeFactory(); }