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

2015/10/7

Problems removing an Object

sidetracked sidetracked

2015/10/7

#
I can't get the HealthDot to be removed.
    public void checkHealth()
    {
        World myWorld = getWorld();
        Actor myDot = new HealthDot1();

        if (health == 3)
        {
            myWorld.addObject(new HealthDot1(), 10, 10);
            myWorld.addObject(new HealthDot2(), 20, 10);
            myWorld.addObject(new HealthDot3(), 30, 10);
        }

        if (health == 2)
        {
            //Here an error "cannot find symbol - method HealthDot1()" occurs.
            myWorld.removeObject(HealthDot1());
        }

        if (health == 1)
        {

        }

        if (health == 0)
        {
            getWorld().addObject(new GameOver(), 200, 200);
            Greenfoot.stop();
        }
    }
Collin Collin

2015/10/7

#
Try this:
public void checkHealth()
{
    World myWorld = getWorld();
    Actor myDot = new HealthDot1();
 
    if (health == 3)
    {
        myWorld.addObject(new HealthDot1(), 10, 10);
        myWorld.addObject(new HealthDot2(), 20, 10);
        myWorld.addObject(new HealthDot3(), 30, 10);
    }
 
    if (health == 2)
    {
        myWorld.removeObject(myDot);
    }
 
    if (health == 1)
    {
 
    }
 
    if (health == 0)
    {
        getWorld().addObject(new GameOver(), 200, 200);
        Greenfoot.stop();
    }
}
since you have already declared a variable
danpost danpost

2015/10/7

#
@Collin, the change you suggested may remove the error; but the object created at line 4 is never added into the world, so you cannot remove it from the world. That, and there appears to be more basic issues involved. @sidetracked, maybe my Value Display Tutorial might be of some help.
You need to login to post a reply.