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

2014/10/15

Need help with remove object

Fulman Fulman

2014/10/15

#
Alright, before I write anything I want to make clear I am really new to programming, or kind of new. So i started out greenfoot and made a small game with a Lobster eating babies. I wanted to add an enemy, so I added an Ant. I want the ant to remove the Lobster when he touches it. The player plays as the Lobster. I tried this code but it did not work.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ant here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ant extends Actor
{
    /**
     * Act - do whatever the ant wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAround();
    }
      public void moveAround()
      {
        move(3);
        if (Greenfoot.getRandomNumber(100) < 10)
        {
           turn(Greenfoot.getRandomNumber(90) - 45);  
        }

    }
    public void eat()
    {
        Actor Lobster;
      Lobster = getOneObjectAtOffset(0, 0, Lobster.class);
        if(Lobster != null)
           {
               World world;
               world = getWorld();
               world.removeObject(Lobster);
               Greenfoot.playSound("eating.wav");
            }
        }  
    }
Thats my ant. Look at the last paragraph and you will understand what i am trying to do. I used the same method as when I made the lobster eat babies. Thanks for any help, Fulman.
Super_Hippo Super_Hippo

2014/10/15

#
Maybe you should add 'eat();' after line 17.
Fulman Fulman

2014/10/15

#
Oh, thank you man, that was the problem. Did not see that i forgot that. :D
You need to login to post a reply.