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

2019/5/25

How to remove an certain Object

TobiMobi TobiMobi

2019/5/25

#
Hello community, First of all sorry for my bad English. I'm new here so I don't know if my information are enough to help me. I have to code a Pacman-game but now I have a problem. That are my classes: In the method eatPacmanlife, I want to code that if the Ghost eats a Pacman the Object LIFE1 gets removed and pacmanlife gets +1. But im not sure how I can remove an certain object. the coordinates of LIFE1 are x=66 ; y=104. I added the Object in the PACMANWORLD, so everytime i start the game the LIFE1 gets automatically spawned ( addObject(new LIFE1(), 66, 104); ). Im not the best in Greenfoot we are only learning the basics. Would be nice if somebody can help me with an easy code. Thank you.
public class GHOSTS extends Actor
{
    int pacmanlife=0;
    /**
     * Act - do whatever the Pacman wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {

    } 

    public boolean FoundPacman()
    {
        Actor pacman = getOneObjectAtOffset(0 ,0 , PACMAN.class);
        if(pacman!=null)
        {
            return true;
        }

        return false;
    }

    public void EatPacman()
    {

        Actor pacman = getOneObjectAtOffset(0 ,0 , PACMAN.class);
        if(pacman!=null)
        {
            getWorld().removeObject(pacman);

        }
    }

    public void eatPacmanLife() 
    {
        if(FoundPacman())
        {
            EatPacman();
            if(pacmanlife==0)
            {
                pacmanlife=pacmanlife+1;
            }
            if(pacmanlife==2)
            {
                pacmanlife=pacmanlife+2;
            }
            if(pacmanlife==3)
            {
                Greenfoot.stop();
            }
        }
    }
}
danpost danpost

2019/5/25

#
You could try:
getWorld().removeObjects(getWorrld().getObjectsAt(66, 104, LIFE1.class));
TobiMobi TobiMobi

2019/5/26

#
Thank you it works.
You need to login to post a reply.