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

2018/10/31

How Do You Remove an Actor?

CosmicCaleb CosmicCaleb

2018/10/31

#
You can remove an actor by using the code
1
getWorld().removeObject(this);
but this has given me the error
Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
So I was wondering if there was another way to remove yourself or a way to not make that error pop up. Btw, the error only pops up if I have a situation similar to this: you have a bullet and it hits an object, then you replace the bullet with a blood splat. In this situation that error pops up
danpost danpost

2018/10/31

#
CosmicCaleb wrote...
the error only pops up if I have a situation similar to this: you have a bullet and it hits an object, then you replace the bullet with a blood splat.
Put the blood splat in the world and then have the bullet remove itself. You lose reference to the world, otherwise (unless you place a reference to it in a local variable first).
CosmicCaleb CosmicCaleb

2018/11/1

#
1
2
3
4
5
6
if (isTouching(Car.class))
        {
            removeTouching(Car.class);
            addSplat(getX(), getY());
            getWorld().removeObject(this);
        }
Even if I do this, the error still pops up
danpost danpost

2018/11/1

#
CosmicCaleb wrote...
<< Code Omitted >> Even if I do this, the error still pops up
Where is the addSplat method?
Super_Hippo Super_Hippo

2018/11/1

#
Make sure you don't remove the object earlier in this act cycle or from a different class (the car for example).
danpost danpost

2018/11/1

#
For best help results, please show the current complete class and copy/paste the entire error output created from that code.
You need to login to post a reply.