So, I would like to delete an object of the "Crab" class when it reaches the end of the world, but it doesn't quite work... have any ideas? Here's my code:
Thanks in advance!
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Crabs are Lobster food
*/
public class Crab extends Actor
{
/**
* Act - do whatever the Crab wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
isInWorld();
crabMovement();
}
public void crabMovement ()
{
int crabRandomMoveChoice = Greenfoot.getRandomNumber(4); //makes crab move in random directions
if(crabRandomMoveChoice == 0 && isInWorld()){
setLocation(getX()+4, getY());
}
if(crabRandomMoveChoice == 1 && isInWorld()){
setLocation(getX()-4, getY());
}
if(crabRandomMoveChoice == 2 && isInWorld()){
setLocation(getX(), getY()+4);
}
if(crabRandomMoveChoice == 3 && isInWorld()){
setLocation(getX()+4, getY()-4);
}
}
public boolean isInWorld()
{
if(getX() <= 5 || getX() >= getWorld().getWidth() -5) //removes crab if he touches the edge
{
getWorld().removeObjects(getWorld().getObjects(Crab.class));
return false;
}
if (getY() <= 5 || getY() >= getWorld().getHeight() -5)
{
getWorld().removeObjects(getWorld().getObjects(Crab.class));
return false;
}
else //if he doesn't touch the edge, continue moving
{
crabMovement();
return false;
}
}
}

