This site requires JavaScript, please enable it in your browser!
Greenfoot back
Abomb1126
Abomb1126 wrote ...

2014/1/8

Not calling a class when it should

Abomb1126 Abomb1126

2014/1/8

#
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:
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();
    }
}
Crab.class
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();
        }
    }
}
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
danpost danpost

2014/1/8

#
You are trying to save the world as a World object (see line 7 of the Crab class). Unless you tell the system that it is indeed a CrabWorld object, it will not look in that class for the method. Another thing about line 7, 'getWorld' will always return 'null' when the field is initialized. It remains null up to and beyond the completion of construction of the actor. Only after it has been completely constructed and added into a world will it return a non-null World object. You can use:
private CrabWorld world; // for line 7
// and add this method
public void addedToWorld(World w)
{
    world = (CrabWorld) w;
}
The addedToWorld method is part of the Actor class (like the act method) and is only called once each time the actor is inserted into a world and will save the current world in the 'world' field as a CrabWorld object. Also, line 80 of the CrabWorld class is trying to call the 'removeObject' method on an Actor, where it can only be called on a World object and needs the actor as an argument
removeObject(playerCrab);
The code is in the CrabWorld class and does not need the 'this' object prefix.
You need to login to post a reply.