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

2016/3/29

Help Fixing Error

ZoeSolosobal ZoeSolosobal

2016/3/29

#
public class Mito extends Animal { /** * 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#1 if (isAtEdge() ) { turn(19); } move(2); // 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#2 move(1); if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } //Problem # 3 if (Greenfoot.isKeyDown("x")) { Greenfoot.playSound("Boing.wav"); } if (Greenfoot.isKeyDown("y")) { Greenfoot.playSound("Bam.wav"); } } public void eat() { Actor FishFoodPellets; FishFoodPellets = getOneObjectAtOffset(0, 0, FishFoodPellets.class); if (FishFoodPellets != null) { World world; world = getWorld(); world.removeObject(FishFoodPellets); } } public void lookForCode() { if (canSee(FishFoodPellets.class)) { eat(FishFoodPellets.class); Greenfoot.playSound("Chomp.wav"); } } /** * Return true if Mito can see an object of the FishFoodPellets class. Return false if there is no FishFoodPellet object there. */ public boolean canSee (Class FishFoodPellets) { Actor FishFoodPellets = getOneObjectAtOffset(0, 0, FishFoodPellets.class); return FishFoodPellets !=null; } /** * Eat an object of class "FishFoodPellets" if there is such an object in our current location. */ public void eat(Class FishFoodPellets) { Actor FishFoodPellets = getOneObjectAtOffset(0, 0, FishFoodPellets.class); if (FishFoodPellets != null) { getWorld().removeObject(FishFoodPellets); } } } How do I fix the error: "Actor FishFoodPellets = getOneObjectAtOffset(0, 0, FishFoodPellets.class);" Greenfoot says for the first one, "Variable FishFoodPellets is alread defined in method eat(java.lang.Class)" and the second one says, "Variable FishFoodPellets is alread defined in method canSee(java.lang.class)" I
danpost danpost

2016/3/29

#
The parameter in both methods, 'canSee' and 'eat', define a variable named FishFoodPellets that is of type Class. Then inside the method you try to declare the same variable name to be of type Actor. You cannot have the same variable name declared twice (even if they are declared as the same type). Anyway, there is no reason for these methods in this class. The 'canSee' and 'eat' methods are defined in the Animal class and given their implementation there. No method after the act method in your Mito class code is being called and should be removed (thereby, removing the errors as well). You can then add code to the act method that uses the 'canSeee' and 'eat' methods of the Animal class as if they were part of the Mito class (this is called inheritance -- all non-private methods and fields in superclasses and interfaces of a class are "part" of that class).
ZoeSolosobal ZoeSolosobal

2016/3/29

#
Thank you so much!! I have been trying to figure it out for a while.
You need to login to post a reply.