this is the coding
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class babyDolphin here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class babyDolphin extends Actor
{
private static final int EAST = 0;
private static final int WEST = 1;
private int direction;
/**
* Act - do whatever the babyDolphin wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if(Greenfoot.mouseClicked(this))
{
getWorld().removeObject(this);
System.out.println("mouse clicked on baby Dolphin!");
}
if ( canMove() )
{
move();
}
else
{
turnRandom();
}
}
public void move()
{
if (!canMove()) {
return;
}
switch(direction) {
case EAST :
setLocation(getX() + 1, getY());
break;
case WEST :
setLocation(getX() - 1, getY());
break;
}
}
public boolean canMove()
{
int x = getX();
int y = getY();
switch(direction) {
case EAST :
x++;
break;
case WEST :
x--;
break;
}
if ( x == 599 || x == 0 )
{
return false;
}
else
{
return true;
}
}
public void turnRandom()
{
// get a random number between 0 and 3...
int turns = Greenfoot.getRandomNumber(10);
// ...an turn left that many times.
for(int i=0; i<turns; i++) {
turnLeft();
}
}
public void turnLeft()
{
switch(direction) {
case EAST :
setDirection(WEST);
break;
case WEST :
setDirection(EAST);
break;
}
}
public void setDirection(int direction)
{
this.direction = direction;
switch(direction) {
case EAST :
setRotation(0);
break;
case WEST :
setRotation(0);
break;
default :
break;
}
}
{
}
}
Try switching the 'if (canMove())' block with the 'if(Greenfoot.mouseClicked(this))' block in the act method (notice I stated the blocks--not the statements).
The way you have it now, it is possible that the actor is removed from the world in the first block and errors creep up in the second because you cannot get the actors location while not in the world.