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

2023/1/7

Actor has been removed from the world error

Makuro Makuro

2023/1/7

#
So I got Actor has been removed error whenever the projectile hits the edge of the world and is destroyed I've tried putting return; after the removeObject line but it is not working. Here is the superclass and subclass of the projectile:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Projectile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Projectile extends Actor
{
    /**
     * Act - do whatever the Projectile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public GreenfootImage image;
    public static int playerDamage;
    public static int enemyDamage;
    
    public Projectile(){

    }
    Projectile projectile;
    public void act()
    {

    }
    
    public void Move(){
        projectile = this;
        if(isAtEdge()){
            getWorld().removeObject(projectile);  
            return;
        }
        setLocation(getX(),getY()-2);
    }
    
    public void SetAsset(){
        image = getImage();
        image.scale(10,10);
        setImage(image);
    }
    
    public void DealDamage(int playerDamage, int enemyDamage){
        if(!isAtEdge()){
            Actor player = getOneIntersectingObject(Player.class);
            Actor enemy = getOneIntersectingObject(EnemyPlanes.class);
            if(player!=null){
              System.out.println(enemyDamage);
              getWorld().removeObject(this);
              return;
            }else if(enemy != null){
              System.out.println(enemyDamage);
              getWorld().removeObject(this);
              return;
            }
            else return;    
        }

    }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player_Projectile here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player_Projectile extends Projectile
{
    /**
     * Act - do whatever the Player_Projectile wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Player_Projectile(){
        SetAsset();
        playerDamage = 5;
    }
    public void act()
    {
        Move();
        DealDamage(playerDamage, 0); 
    }
}
danpost danpost

2023/1/8

#
Makuro wrote...
So I got Actor has been removed error whenever the projectile hits the edge of the world and is destroyed I've tried putting return; after the removeObject line but it is not working. Here is the superclass and subclass of the projectile: << Code Omitted >>
Putting a return in a method only prevents further code from being executed from within that method. It does not prevent other code from being executed from the method where that method was called from. A return in the Move method has execution revert back to the act method and the next line in act is executed. Place the following line between the two lines in act of Player_Projectile class:
if (getWorld() == null) return;
which says if the actor is not in a world, quit act.
danpost danpost

2023/1/8

#
Another way to "solve" this is to change line 44 in Projectile class to the following:
if (getWorld() != null && !isAtEdge()){
You need to login to post a reply.