I have the CrabWorld class that I added two methods called respawnCrab, and killCrab. Both are public, and non-static. Now, I have it when the lobster eats the playerCrab (an instance of the Crab class declared in the CrabWorld), the playerCrabs method looseLife is decremented.
My three classes are
CrabWorld.class:
Crab.class
The problem I'm having is when the crab "looses a life" (looseLife() is called). It should check to see if there is a life left, then either, have the world kill the crab, or respawn it. When I compile the code, it gives me an error that reads "cannot find symbol -method respawnCrab". I clearly have it defined in the CrabWorld class, and I have the CrabWorld retrieved as "world" using the getWorld method of all Actors. Can someone please tell me why I still get an error?
Thanks
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class CrabWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CrabWorld extends World
{
public Crab playerCrab = new Crab();
private Counter score = new Counter("Score: ");
private int numbOfWorms = 0;
/**
* Constructor for objects of class CrabWorld.
*
*/
public CrabWorld()
{
super(560, 560, 1);
prepare();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void prepare()
{
addObject(score, 100, 540);
addObject(playerCrab, 280, 280);
addObject(new Lobster(), 100, 100);
}
/**
* Grabs the playerCrab instance.
*/
public Crab getCrab()
{
return playerCrab;
}
/**
* Randomly places worms at random periods
*/
public void act()
{
if(numbOfWorms < 50){
if(Greenfoot.getRandomNumber(100)<0.5){
addObject(new Worm(), Greenfoot.getRandomNumber(540)+10, Greenfoot.getRandomNumber(540)+10);
numbOfWorms++;
}
}
}
public void eatenWorm()
{
score.add(10);
numbOfWorms--;
}
public int getNumbOfWorms()
{
return numbOfWorms;
}
public void respawnCrab()
{
playerCrab.setLocation(280,280);
playerCrab.setRotation(0);
}
public void killCrab()
{
playerCrab.removeObject();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Crab extends Actor
{
private int wormsEaten = 0;
private int lives = 3;
World world = getWorld();
/**
* 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()
{
moveAndTurn();
eat();
}
/**
* Determines the key pressed and moves/turns the crab.
*/
public void moveAndTurn()
{
move(3);
if(Greenfoot.isKeyDown("a")){
turn(3);
}
if(Greenfoot.isKeyDown("d")){
turn(-3);
}
}
/**
* Detects if the worm is close to the crab, and if so, eats it.
*/
public void eat()
{
Actor worm;
worm = getOneObjectAtOffset(0,0,Worm.class);
World world = getWorld();
if(worm != null)
{
world.removeObject(worm);
Greenfoot.playSound("eating.wav");
wormsEaten++;
((CrabWorld) getWorld()).eatenWorm();
}
}
/**
* Returns the number of worms eaten.
*/
public int getWormsEaten()
{
return wormsEaten;
}
/**
* Returns the number the lives the crab has left.
*/
public int getLivesCount()
{
return lives;
}
/**
* Looses a life when called
*/
public void looseLife()
{
lives--;
if(lives>0){
world.respawnCrab();
}else{
world.killCrab();
}
}
}
