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

2022/2/23

Error greenfoot.ActorRemovedFromWorld fix?

asiwi asiwi

2022/2/23

#
I have a problem. Im currently programming a game as a school project. In my game, there is a hero an enemies, which the hero can kill after 3 hits. However, when I kill an enemy it gives me the following error: greenfoot.ActorRemovedFromWorld at greenfoot.World.removeObject(World.java:466) at Gegner.hit(Gegner.java:136) at Gegner.act(Gegner.java:25) ... 4 more How can i fix this? Nothing I have tried worked so far.
    public void act() 
    {
        count++;
        hit();
        move();
        falling();
        checkFalling();
        onPlatform();
        randomSpawn();
        randomjump();
    }  

    public void move()
    {
        
        if (count < 120)
        setLocation(getX() + speed, getY());
        else{
        speed = -speed;
        getImage().mirrorHorizontally();
        count = 0;
        }
    }
    public void falling()
    {
        this.setLocation(this.getX(), this.getY() + verticalSpeed);
         verticalSpeed = verticalSpeed + 1;
    }
     public void checkFalling()
    {
        if(onPlatform())
        {
            verticalSpeed = 0;
        }
        else
        {
            falling();
        }
    }
    public boolean onPlatform() 
    {
        Actor under = getOneObjectAtOffset (0, getImage().getHeight()/2, Ground.class);
         return under != null;
    }
    public void randomSpawn()
    {
        if (getWorld() != null  && getWorld().getObjects(Gegner.class).size() < 4 && Greenfoot.getRandomNumber(200) == 1)
        {
            Actor Gegner = new Gegner();
            getWorld().addObject(Gegner, 50, 187);
            
            if (Greenfoot.getRandomNumber(2) == 1) 
            {
                Gegner.setLocation(630, 50);
                
            }
            if (Greenfoot.getRandomNumber(2) == 1) 
            {
                Gegner.setLocation(Gegner.getX(), 450);
                Gegner.setRotation(-Gegner.getRotation());
            }
            
        }
    }
    public void randomjump()
   {
       if( onPlatform() && Greenfoot.getRandomNumber(80) ==3)
       {
           jump();
        }
    }
    public void jump()  
    { 
           verticalSpeed = 2 +jumpStrength;
           falling();
    }
    
    public void hit()
    {   Actor projektil = getOneIntersectingObject(Projektil.class);
        if(projektil != null && !hit)
        {
            hp--;
            hit = true;
            getWorld().removeObject(projektil);
        }
        else if(!isTouching(Projektil.class))
        {
            hit = false;
        }
        
        if (hp <= 0) {
            
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2022/2/23

#
Look at line 93 in the hit method. It potentially removes this actor from the world. Now, look back at the act method. Line 4 is where the hit method is called. Every method called after line 4 requires the actor to be in the world. The only method called after line 4 in act that is immune from this error is the randomSpawn method as the condition that the actor be in a world is verified in line 47. This condition can be moved to after line 4 in the act method to ensure the actor is in the world so the methods called after line 4 will process without the error. Insert the following after line 4:
if (getWorld() == null) return;
and change line 47 to:
if (getWorld().getObjects(Gegner.class).size() < 4 && Greenfoot.getRandomNumber(200) == 1)
The insertion will break out of the act method if the actor is removed from calling the hit method. The modification of line 47 is trivial, but removes an unnecessary check as nothing after the inserted line removes the actor from the world (the actor is currently guaranteed to be in the world when the checkSpawn method is called because of the inserted line).
asiwi asiwi

2022/2/23

#
Thank you very much, it worked like a charm!!
AbdulrazaqAS AbdulrazaqAS

2022/2/25

#
I just got the error and after trying the solution provided, it worked. Thank you.
You need to login to post a reply.