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

2022/2/22

Remove actor from world

NielsProgamer NielsProgamer

2022/2/22

#
I have a question. I made a sidescroller game with a gun. Enemys are moving from right to left en there is a actor on the left who shoots the enemys. I have made two different remove statements. One which removes the enemy if its at x= 0. And the other one removes when the bullet hits one of the enemys. Now there is a problem, I think its not possible to use remove with to different statements. I have seen something on this site about return but that also doesnt work. Can someone explain what I need to do? This is my code for one of the enemys:
        public void removeAtZero()
    {
        if ( getX() == 0)
        {
            getWorld().removeObject(this);
            return;
        }       
    }
    public void removeWhenShot()
    {
        if ( isTouching(Kogel.class))
        {
            getWorld().removeObject(this); 
            return;
        }
    }
NielsProgamer NielsProgamer

2022/2/22

#
This is the error: java.lang.IllegalStateException: Actor has been removed from the world. at greenfoot.Actor.failIfNotInWorld(Actor.java:722) at greenfoot.Actor.isTouching(Actor.java:987) at Paarse_Minion.removeWhenShot(Paarse_Minion.java:35) at Paarse_Minion.act(Paarse_Minion.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:567) at greenfoot.core.Simulation.runOneLoop(Simulation.java:530) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) Caused by: greenfoot.ActorRemovedFromWorld at greenfoot.World.removeObject(World.java:466) at Paarse_Minion.removeAtZero(Paarse_Minion.java:29) at Paarse_Minion.act(Paarse_Minion.java:22) ... 4 more
lehrerfreund lehrerfreund

2022/2/22

#
Why do you use different methods for this? Possible would be:
public void checkRemove()
{
  if( ( getX() == 0) || isTouching(Kogel.class) )
  {
    getWorld(removeObject(this));
  }
}
NielsProgamer NielsProgamer

2022/2/22

#
Thanks! it works. Didnt think about that.
You need to login to post a reply.