I need to create a "penalty" so that when the Crab hits the edge of the world, another Worm is added to a random place in the world. my world is 560x560. Ive tried using addObject but i cant figure out how to make it work in the Crab class and the World wont recognize atWorldEdge. i can provide other codes if needed
public class Crab extends Animal
{
private GreenfootImage image1;
private GreenfootImage image2;
private int wormsEaten;
private int act;
public Crab()
{
image1 = new GreenfootImage("crab.png");
image2 = new GreenfootImage("crab2.png");
setImage(image1);
wormsEaten = 0;
}
/**
* 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()
{
checkKeypress();
move();
lookForWorm();
switchImage();
}
/**
* Check whether a control key on the keyboard has been pressed.
* If it has, react accordingly.
*/
public void checkKeypress()
{
if (Greenfoot.isKeyDown("left"))
{
turn(-4);
}
if (Greenfoot.isKeyDown("right"))
{
turn(4);
}
if (Greenfoot.isKeyDown("up"))
{
turn(5);
}
if (Greenfoot.isKeyDown("down"))
{
turn(-5);
}
}
public void switchImage()
{
act = act + 1;
if ( act == 3)
{
act = 0;
if ( getImage() == image1 )
{
setImage(image2);
}
else
{
setImage(image1);
}
}
}
/**
* Check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing.
*/
public void lookForWorm()
{
if ( canSee(Worm.class) )
{
eat(Worm.class);
Greenfoot.playSound("slurp.wav");
wormsEaten = wormsEaten + 1;
if (wormsEaten == 8)
{
Greenfoot.playSound("fanfare.wav");
Greenfoot.stop();
}
}
}
public void turnAtEdge()
{
if (atWorldEdge() )
{
turn(42);
move();
}
}
}
