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

2012/5/5

Not sure whats wrong

Swedishsam Swedishsam

2012/5/5

#
The problem relates to world.placeFood(1); and the error is = cannot find symbol - method placeberry(int) i'm trying to make it so that when the snake eats a berry it removes that berry and adds another one to the world.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class SnakeWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SnakeWorld extends World
{
    private berry  berry1;
    /**
     * Constructor for objects of class SnakeWorld.
     * 
     */
    public SnakeWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        setBackground("Black.jpg");
        prepare();
        placeberry(1);
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Snake snake = new Snake();
        addObject(snake, 183, 205);        
    }   

    {
       Greenfoot.playSound("skrillex.mp3");
    }
        public void placeberry(int amountOfFood)
    {
        for (int i = 0; i < amountOfFood; i++)
        {
            addObject(new berry(), Greenfoot.getRandomNumber(499)+1,  Greenfoot.getRandomNumber(419)+1);
        }     
    } 
    
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Snake here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Snake extends Actor
{

    /**
     * Act - do whatever the Snake wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
          moveAndTurn();
          eat();

      
          if (getX() <=5 || getX() >= getWorld().getWidth() -5)
          { 
           getWorld().removeObject(this);

           return;
          }
          if (getY() <=5 || getY() >= getWorld().getHeight() -5)
          { 
           getWorld().removeObject(this);

           return;
          }

    } 
    
    
    
    public void moveAndTurn()
    {
        move(2);
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
        }
    }
      
        
        public void eat()
        {
        Actor berry;
        berry = getOneObjectAtOffset(0, 0, berry.class);
        if (berry != null)
        {
            World world;
            world = getWorld();
            world.removeObject(berry);
            world.placeFood(1);
        }        
    }     
    }
danpost danpost

2012/5/5

#
It appears your world does not have a method called placeFood(int), maybe you meant placeberry(int).
Swedishsam Swedishsam

2012/5/5

#
i replaced placeFood(int) with placeberry(int) still getting the same error as before.
danpost danpost

2012/5/5

#
Is line 70 now: world.placeberry(1); ???
Swedishsam Swedishsam

2012/5/5

#
danpost wrote...
Is line 70 now: world.placeberry(1); ???
Line 70 is now world.placeberry(1);
Swedishsam Swedishsam

2012/5/5

#
and the error is still the same
danpost danpost

2012/5/5

#
What exactly does the error message say, and what line is highlighted?
danpost danpost

2012/5/5

#
NVM, I know what is going on! One second.
danpost danpost

2012/5/5

#
In the Snake class, in the eat() method, you have in lines 67 & 68:
World world;
world = getWorld();
which returns a general World class object (not a SnakeWorld object where the method you are trying to call is located). Instead, use
danpost danpost

2012/5/5

#
SnakeWorld world;
world = (SnakeWorld) getWorld();
Swedishsam Swedishsam

2012/5/5

#
Thank you very much, my problem has now been resolved.
You need to login to post a reply.