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

2019/5/17

removeObject() method won't work

woody22 woody22

2019/5/17

#
before this code i set lives to 3, and whenever i hit an object 1 gets subtracted from lives. Using other methods works (such as adding or removing other object) so I'm sure its an issue with removing the lives specifically.
 public void health()
    {
        Lives life1=new Lives();
        Lives life2=new Lives();
        Lives life3=new Lives();
        if (lives==3)
        {
           getWorld().addObject(life1,20,20);
            getWorld().addObject(life2,60,20);
            getWorld().addObject(life3,100, 20); 
        }
        if (lives==2)
        {
           getWorld().removeObject(life3); 
        }
        if (lives==1)
        {
           getWorld().removeObject(life2); 
        }
        if (lives==0)
        {
            getWorld().removeObject(life1);
        }
        
    }
Super_Hippo Super_Hippo

2019/5/17

#
Move lines 3-5 outside the method. Right now you are creating three new Lives objects whenever the method is called. The ones you remove are never in the world.
woody22 woody22

2019/5/20

#
Alright that worked, thank you!
You need to login to post a reply.