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

2012/9/8

Crab World, need help

TasMath TasMath

2012/9/8

#
Hi, I am a total fail at programming, and i have to learn something so i can pass my class. I am stuck i this CrabWorld were i have to make a method that will return the number of worms, my crab has eaten. public class Crab extends Animal { public void act() { moveAndTurn(); eat(); } public void moveAndTurn() { move(4); if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } } public void eat() { Actor worm; worm = getOneObjectAtOffset(0, 0, Worm.class); if (worm != null) { World world; world =getWorld(); world.removeObject(worm); } } } so far this is what the crab looks like, i don't know if everything is right, but the crab is moving and eating. now i just need to see how many worms.. Please i need help... and please easy on the java-language, i started java 2 weeks ago. :O)
erdelf erdelf

2012/9/8

#
you just need an integer to count that:
public class Crab extends Animal 
{ 
   private int worms;
   public Crab()
   {
        worms = 0;
    }
   public void act() 
   {
         moveAndTurn(); 
         eat();
   } 
   public void moveAndTurn() 
   { 
          move(4); 
          if (Greenfoot.isKeyDown("left")) 
          { 
                turn(-3); 
          } 
          if (Greenfoot.isKeyDown("right")) 
          { 
                turn(3); 
           } 
   }
   public void eat() 
   { 
        Actor worm; 
        worm = getOneObjectAtOffset(0, 0, Worm.class); 
        if (worm != null) 
        { 
            World world; world =getWorld(); 
            world.removeObject(worm); 
            worms++;
         } 
   } 
   public int getWorms()
   {
         return worms;
   }
}
oh and next time when you write a code press the code button below the reply box
TasMath TasMath

2012/9/8

#
THANK you, amazing.. :)
You need to login to post a reply.