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

2016/3/30

Where is the error?

ZoeSolosobal ZoeSolosobal

2016/3/30

#
The object, Mito is supposed to eat the FishFoodPellets, but I cannot find where the error in my code is that is not making this happen. If you could help me that'd be awesome. Thanks! Code:
public class Mito extends Animal
{
    private int wormsEaten;
    /**
     * Act - do whatever the Mito wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       
       // Problem#3 The defined method I used is isAtEdge. The code I used it if (isAtEdge() 0 { turn(19); 
       //Problem #1
       move(5);
       if (Greenfoot.isKeyDown("a"))
       {
           turn(2);
        }
       move(2);
       if (Greenfoot.isKeyDown("b"))
       {
           turn(5);
        }
      //Problem # 3
       if (Greenfoot.isKeyDown("x"))
       {
           Greenfoot.playSound("Boing.wav");
       }
       if (Greenfoot.isKeyDown("y"))
       {
           Greenfoot.playSound("Bam.wav");
       }
       
    }
    public void eatFishFoodPellets()
    {
        Actor FishFoodPellets;
        FishFoodPellets = getOneObjectAtOffset(0, 0, FishFoodPellets.class);
        if (FishFoodPellets != null)
        {
            World world;
            world = getWorld();
            world.removeObject(FishFoodPellets);
        }
    }
    
    public void lookForFishFoodPellets()
    {
        if (isTouching(FishFoodPellets.class))
        {
            removeTouching(FishFoodPellets.class);
            Greenfoot.playSound("Chomp.wav");
            
            wormsEaten = wormsEaten + 1;
        }

    }
    /**
     * Return true if Mito can see an object of the FishFoodPellets class. Return false if there is no FishFoodPellet object there.
     */
    public boolean canSeeFishFoodPellets()
    {
        Actor FishFoodPellets = getOneObjectAtOffset(0, 0, FishFoodPellets.class);
        return FishFoodPellets !=null;
    }
    public void turnAtEdge()
    {
        if (isAtEdge())
        {
            turn(19);
        }
    }
    public void checkKeypress()
    {
        if (Greenfoot.isKeyDown("left"))
       {
           turn(-3);
       }
        if (Greenfoot.isKeyDown("right"))
       {
            turn(3);
       }
    }
}
danpost danpost

2016/3/31

#
None of the methods, 'checkForKeypress', 'turnAtEdge', 'canSeeFishFoodPellets', 'lookForFishFoodPellets' and 'eatFishFoodPellets', are linked to the 'act' method in any way. You need to call a method for it to execute (greenfoot calls the 'act' method).
You need to login to post a reply.