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

2021/3/22

How to remove an actor already in the world from another actor?

oofitsme oofitsme

2021/3/22

#
Hello. I am working on a school project and all I want to is have an actor remove itself and another actor already in the world. To have the actor remove itself I just used getWorld().removeObject(this); and for the other actor I used getWorld().removeObject(orderSlip2);. But when I run my code and click the OrderSlip1 actor, it removes neither object. I'm really just trying to make a simple mouse pressing game that adds and removes objects. Everything else I've tried to do in Greenfoot has either been too ambitious or I'm not using the API correctly. It's super frustrating.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class OrderSlip1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OrderSlip1 extends Actor
{  
   public OrderSlip2 orderSlip2;
    /**
     * Act - do whatever the OrderSlip1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    { 
        orderSlip2 = new OrderSlip2();
        if (Greenfoot.mousePressed(this) == true) {
          getWorld().removeObject(this);
          getWorld().removeObject(orderSlip2);
          
     }
  }
}
Super_Hippo Super_Hippo

2021/3/22

#
You should get an error message when you click on the object. Line 20 removes the object from the world. It isn’t in a world anymore, so “getWorld()” in line 21 returns “null” and you can’t call methods on “null”. You can fix this by swapping lines 20 and 21. Then the object should remove itself. It shouldn’t remove the other object though. The object which it is trying to remove from the world is the one you create in line 18. It isn’t one which might be in the world. To remove the one in the world, you need to find a reference to that one. Depending on what exactly you are trying to achieve, you can either remove all objects of class OrderSlip2 from the world or the first one in the list. Alternatively, you can save a reference to a specific object somewhere, for example in the world.
public void act()
{
    if (Greenfoot.mousePressed(this))
    {
        getWorld().removeObjects(getWorld().getObjects(OrderSlip2.class));
        getWorld().removeObject(this);
    }
}
public void act()
    {
        if (Greenfoot.mousePressed(this))
        {
            if (!getWorld().getObjects(OrderSlip2.class).isEmpty())
            {
                getWorld().removeObject(getWorld().getObjects(OrderSlip2.class).get(0));
            }
            getWorld().removeObject(this); //put it into the if-block to let it stay in the world if there is no OrderSlip2 object in the world
        }
    }
oofitsme oofitsme

2021/3/22

#
Super_Hippo wrote...
You should get an error message when you click on the object. Line 20 removes the object from the world. It isn’t in a world anymore, so “getWorld()” in line 21 returns “null” and you can’t call methods on “null”. You can fix this by swapping lines 20 and 21. Then the object should remove itself. It shouldn’t remove the other object though. The object which it is trying to remove from the world is the one you create in line 18. It isn’t one which might be in the world. To remove the one in the world, you need to find a reference to that one. Depending on what exactly you are trying to achieve, you can either remove all objects of class OrderSlip2 from the world or the first one in the list. Alternatively, you can save a reference to a specific object somewhere, for example in the world. Thank you so much!! It works :))
public void act()
{
    if (Greenfoot.mousePressed(this))
    {
        getWorld().removeObjects(getWorld().getObjects(OrderSlip2.class));
        getWorld().removeObject(this);
    }
}
public void act()
    {
        if (Greenfoot.mousePressed(this))
        {
            if (!getWorld().getObjects(OrderSlip2.class).isEmpty())
            {
                getWorld().removeObject(getWorld().getObjects(OrderSlip2.class).get(0));
            }
            getWorld().removeObject(this); //put it into the if-block to let it stay in the world if there is no OrderSlip2 object in the world
        }
    }
You need to login to post a reply.