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

2014/5/22

Ending a programme

1
2
11frendash 11frendash

2014/6/18

#
wont exept the leavesEaten, please help
danpost danpost

2014/6/18

#
The variable name is not declared as a field within the class, so, again, the compiler cannot find it.
11frendash 11frendash

2014/6/18

#
something like... leavesEaten = 0;
danpost danpost

2014/6/18

#
I suggest you check out the Java tutorial page on Declaring Member Variables in the Classes and Objects trail. Something like:
import greenfoot.*;

public class Wombat extends Animal
{
    private int leavesEaten = 0; // declare it here

    public void act()
    {
        // etc.
11frendash 11frendash

2014/6/18

#
ok, will do
11frendash 11frendash

2014/6/18

#
so many problems!
11frendash 11frendash

2014/6/19

#
sorry again but it wont except the Home home = newHome(); its in the wombat class by the way
import greenfoot.*;
import java.awt.Color;
public class Wombat extends Animal
{
   private int leavesEaten = 0;
   public void act()
   {
   move();
   checkKeypress();
   
       if (canSee(Leaf.class))
       {
           eat(Leaf.class);
           leavesEaten = leavesEaten + 1;
       }
                    
       if (canSee(Ant.class))
       {
           eat(Ant.class);
           
       }
   }

   public void GameOver(int score)   
   {    
       Greenfoot.stop();    
       GreenfootImage text = new GreenfootImage("Game Over\nCoins: " + score, 40, Color.RED, new Color(0, 0, 0, 0));    
       GreenfootImage image = new GreenfootImage(text.getWidth() + 20, text.getHeight() + 20);    
       image.setColor(new Color(255, 255, 255, 128));    
       image.fill();    
       image.drawImage(text, 10, 10);    
       setImage(image);
       Home home = new Home();
       Greenfoot.setWorld(home);
    }

   
   public void eat()
   {
       Actor Ant;
       Ant = getOneObjectAtOffset(0, 0, Ant.class);
       if (Ant != null)
       {
           World world;
           world = getWorld();
           world.removeObject(Ant);
           Greenfoot.playSound("slurp.wav");
       }
       
       {  
        Greenfoot.stop();  
        return;  
       }
   } 
    
   public void lookForAnt()
   {
       if (canSee(Ant.class))
       {
           eat(Ant.class);
           Greenfoot.playSound("slurp.wav");
       }

       if (canSee(Leaf.class))
       {
            eat(Leaf.class);
            Greenfoot.playSound("slurp.wav");
       }
   }
        
   public void checkKeypress()
   {
        if(Greenfoot.isKeyDown("left"))
        {
            turn(-4);
        }
            
        if(Greenfoot.isKeyDown("right"))
        {
            turn(4);
        }
        
    }
}
11frendash 11frendash

2014/6/19

#
24.   public void GameOver(int score)     
25.   {      
26.       Greenfoot.stop();      
27.       GreenfootImage text = new GreenfootImage("Game Over\nCoins: " + score, 40, Color.RED, new Color(0, 0, 0, 0));      
28.       GreenfootImage image = new GreenfootImage(text.getWidth() + 20, text.getHeight() + 20);      
29.       image.setColor(new Color(255, 255, 255, 128));      
30.       image.fill();      
31.       image.drawImage(text, 10, 10);      
32.       setImage(image);  
33.       Home home = new Home();  
34.       Greenfoot.setWorld(home);  
35.    } 
danpost danpost

2014/6/19

#
In the Wombat class, remove lines 33 and 34 (you did not have anything like that to begin with; and I think they were given as example code). Next, change lines 49 through 53 to the following:
if (leavesEaten == 6)
{
    GameOver(leavesEaten);
}
Also, you should probably do something about the redundancy of the code in your 'lookForAnt' method (which is similar, but different, in the act method). Maybe by changing the method name to 'lookForFood' and adding line 14 within the method and replacing lines 10 through 21 with 'lookForFood();'.
11frendash 11frendash

2014/6/19

#
yeah ok seems to be working, also if it works it stays. thats my boring motto, hahaha
You need to login to post a reply.
1
2