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

2021/3/23

Need Help with removing object

Max0309 Max0309

2021/3/23

#
So, hi there. I have to programm a game until tomorrow but I got a huge problem. There are Enemies, which I can kill, and then they despawn. Those Enemies would move if they would be alive. The thing is, that at the moment, I shoot them, they despawn and then I get an error, that there is an object, that does not exist, but try to move. Thats the code of that Enemy:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot und MouseInfo)

/**
 * Ergänzen Sie hier eine Beschreibung für die Klasse enemy.
 * 
 * @author (Ihr Name) 
 * @version (eine Versionsnummer oder ein Datum)
 */
public class Enemy extends Actor
{
    static int died = 0;
    
    /**
     * Act - tut, was auch immer enemy tun will. Diese Methode wird aufgerufen, 
     * sobald der 'Act' oder 'Run' Button in der Umgebung angeklickt werden. 
     */
    
    public void act() 
    {
        if(checkHit()){
          destroy();
          died++;
        }
        int y = getY();
        int x = getX();

        x = x+1;
        setLocation(x, getY());
     }
    public boolean checkHit(){
        if(getIntersectingObjects(DownBullet.class).size() > 0) {
            return true;
        }
        if(getIntersectingObjects(UpperBullet.class).size() > 0) {
            return true;
        }
        if(getIntersectingObjects(LeftBullet.class).size() > 0) {
            return true;
        }
        if(getIntersectingObjects(RightBullet.class).size() > 0) {
            return true;
        }
        return false;
    }
    private void destroy() {
        getWorld().removeObject(this);
    }
    
}
If someone wants to know for what there is that died int, it's only for the single worlds to count how many enemies died.
Gbasire Gbasire

2021/3/23

#
I think that's because there's the "died++" and the setLocation after the "destroy()". The game will first use the destroy() method, removing the object, and then increasing the "died" and trying to move even though the object is not in the world anymore. Try putting "died++" before "destroy()", and also put your lines with setLocation before the line with destroy(), otherwise it will do the same thing. Basically, you should always check if your object is getting removed at the very end of your methods.
Max0309 Max0309

2021/3/23

#
Well I dont know why I did that, but it worked and I love you. Thank you. If I got other problems, I guess its ok to just write that here?
Gbasire Gbasire

2021/3/24

#
the best would be to re-create a discussion, but you can still ask here too
danpost danpost

2021/3/24

#
Insert the following at line 23:
return;
Or, switch the order around:
public void act() 
{
    int y = getY();
    int x = getX();
    x = x+1;
    setLocation(x, getY());
    if (checkHit())
    {
        destroy();
        died++;
    }
 }
You need to login to post a reply.