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

2019/3/9

Bullet doesn't remove itself at a wall

SemH. SemH.

2019/3/9

#
I made a turret that shoots bullets for my game, but the bullets won't go away at the wall. Greenfoot gives an error. It says: Actor not in world. I don't know how to fix this. Can someone help? This is my code.
public void removeAtEdge()
    {
        if (getX()>595)
        {
            World world = getWorld();
            getWorld().removeObject(this);
        }
    }
    
    public void setLocation(int x, int y)
    {
        int oldX = getX();
        int oldY = getY();
        super.setLocation(x, y);
        if(!getIntersectingObjects(Wall.class).isEmpty())
        {
            World world = getWorld();
            getWorld().removeObject(this); 
            
        }
        
    }
danpost danpost

2019/3/9

#
SemH. wrote...
I made a turret that shoots bullets for my game, but the bullets won't go away at the wall. Greenfoot gives an error. It says: Actor not in world. I don't know how to fix this. Can someone help? This is my code. << Code Omitted >>
Put the following as the 1st line in both given methods:
if (getWorld() == null) return;
SemH. SemH.

2019/3/10

#
Thanks! It worked :)
You need to login to post a reply.