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

2020/12/6

"Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed"

TerminatorHD TerminatorHD

2020/12/6

#
Hello, basicly my game works like this. You're a rocket and you have to collect 4 oil canisters. The entire game works. BUT, after one of the asteroids hits you, the game restarts and that is where I get the syntax error which I don't understand. Could someone help me?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rakete here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rakete extends Spielfiguren
{   //Attribute
    public int aufgesammelteBenzinkanister = 0;
    /**
     * Act - do whatever the Rakete wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        String benzinAnzeige = "Benzinkanister:"+this.aufgesammelteBenzinkanister; //Es wird eine Anzeige eingeblendet, welch die eingesammelten Benzinkanister aufzählt
        getWorld().showText(benzinAnzeige, 200, 300);
        raketeSteuerung();
        raketeBerührtAsteroid();
        spielGewonnen();
        raketeBerührtKanister();
    }    
    
    private void raketeSteuerung(){ //Mit dieser Methode ist es möglich die Rakete im Spiel zu steuern
        if(Greenfoot.isKeyDown("w")){
            this.move(3);
        }
        if(Greenfoot.isKeyDown("a")){
            this.turn(-3);
        }
        if(Greenfoot.isKeyDown("s")){
            this.move(-3);
        }
        if(Greenfoot.isKeyDown("d")){
            this.turn(3);
        }
    }
            
    private void raketeBerührtAsteroid(){ //Berührt ein Asteroid die Rakete verschwindet es und das Spiel ist vorbei
        String gameOver="Deine Rakete wurde zerstört!";
        if(this.isTouching(Asteroid.class)){
            getWorld().showText(gameOver, 640, 60);
            getWorld().removeObject(this); // Die Rakete wird entfernt
            Greenfoot.delay(120); // Das Scenario wird für "120 Schritte" pausiert (Die Länge der Pause ist abhängig vom Geschwindigkeitsregler)
            Greenfoot.setWorld(new MeineWelt());
        }
    }
    
    public void raketeBerührtKanister(){
        if(this.isTouching(Benzinkanister.class)){
            removeTouching(Benzinkanister.class);
            aufgesammelteBenzinkanister = aufgesammelteBenzinkanister + 1;
        }
    }
    
    public void spielGewonnen(){
        if(aufgesammelteBenzinkanister==1){ // Werden 5 Benzinkanister aufgesammelt hat man das Spiel gewonnen
            getWorld().showText("Du hast Gewonnen!", 200, 300); // Siegertext
            Greenfoot.delay(200);
            Greenfoot.setWorld(new MeineWelt()); // Die Welt wird nach einem Delay von "120 Schritten" zurückgesetzt
        }
    }
}
    
    
    
    
    
    
    
    
    

TerminatorHD TerminatorHD

2020/12/6

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
	at greenfoot.Actor.failIfNotInWorld(Actor.java:713)
	at greenfoot.Actor.isTouching(Actor.java:978)
	at Rakete.raketeBerührtKanister(Rakete.java:53)
	at Rakete.act(Rakete.java:24)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
TerminatorHD TerminatorHD

2020/12/6

#
Edit: I found my Issue. Instead using "this.removeTouching(Benzinkanister.class); " I used "getWorld().removeObject(Rakete.class);
You need to login to post a reply.