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:
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.
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);
}
}

