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

2015/1/29

Help, im a noob!

Pr0grammer Pr0grammer

2015/1/29

#
Hello, at the first, I am from Germany and my English isnt that well.. so I have this problem and I hope u can help me to find a solution.. If the shot of my Spaceship collides with and "Asteorid" the shot should be removed. The same should happen, if the shot collides with my World Edge. I receive always this msg in the console:
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:681)
	at greenfoot.Actor.getX(Actor.java:157)
	at Shot.amRandDerWelt(Shot.java:51)
	at Shot.act(Shot.java:34)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)"
"amRandDerWelt()" is an method that our teacher created. It equals this :
public boolean amRandDerWelt(){
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight()- 20)
            return true;
        else
            return false;
    }
Pr0grammer Pr0grammer

2015/1/29

#

 public void act() 
    {
       turn(-90);
        move(10);
        turn(90);
        
        if(kollidiertMit(Asteroid.class)){
            getWorld().removeObject(this);
            
        
    
        }
        if(amRandDerWelt()){
          getWorld().removeObject(this);  
            
        }
    }
     public boolean kollidiertMit(Class c){
        Actor a = getOneObjectAtOffset(0, 0, c);
        return a != null;        
    }
    
danpost danpost

2015/1/30

#
To remove the error, change line 13 to this:
if (getWorld() != null && amRandDerWelt())
which ensures the actor is still in the world before checking world edges. To also remove the asteroid collided with, insert the following at line 8:
getWorld().removeObject(getOneObjectAtOffset(0, 0, Asteroid.class));
You need to login to post a reply.