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

2015/6/11

Every time my actor collides with another actor, the terminal window pops up!

Heneong Heneong

2015/6/11

#
Hi guys, I'm back, facing another problem with my code. So far, my ship functionally fires a laser. The laser is supposed to be removed and the asteroid it hits should also be removed (also works correctly), but for some reason every time the actors collide, the program pauses so I have to press play again and the terminal window pops up saying: 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.getOneIntersectingObject(Actor.java:930) at LaserBeam.hitDetectionThree(LaserBeam.java:43) at LaserBeam.act(LaserBeam.java:21) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Here's my coding for actor "LaserBeam". Could someone explain the problem?
import greenfoot.*;

Thanks for your help!


public class LaserBeam extends Actor
{
    public void act() 
    {
        setLocation(getX()+25, getY());
        hitDetectionOne();
        hitDetectionTwo();
        hitDetectionThree();
        checkSide();
    }    
    public void hitDetectionOne()  
    {
        Actor a = getOneIntersectingObject(AstOne.class);  
        if(a != null)  
        {  
            getWorld().removeObject(a);   
            getWorld().removeObject(this);
        }
    }
    public void hitDetectionTwo()  
    {
        Actor b = getOneIntersectingObject(AstTwo.class);  
        if(b != null)  
        {  
            getWorld().removeObject(b);   
            getWorld().removeObject(this);
        }
    }
    public void hitDetectionThree()  
    {
        Actor c = getOneIntersectingObject(AstThree.class);  
        if(c != null)  
        {  
            getWorld().removeObject(c);   
            getWorld().removeObject(this);
        }
    }
    public void checkSide()
    {
        Actor d = getOneIntersectingObject(RightWall.class);  
        if(d != null)  
        {  
            getWorld().removeObject(this);
        }
    }
}
Heneong Heneong

2015/6/11

#
NOTE: hitDetectionOne(), Two(), and Three() are for detecting the three different types of asteroids.
azazeel azazeel

2015/6/11

#
if you use new greenfoot version, you can consider this fantastic method isTouching and
public class LaserBeam extends Actor
{
    public void act() 
    {
        setLocation(getX()+25, getY());
        
		if(isTouching(AstOne.class))
		{
			removeTouching(AstOne.class);   
            getWorld().removeObject(this);
		}
		else if(isTouching(AstTwo.class))
		{
			removeTouching(AstTwo.class);   
            getWorld().removeObject(this);
		}
		else if(isTouching(AstTwo.class))
		{
			removeTouching(AstTwo.class);   
            getWorld().removeObject(this);
		}
		else if(isTouching(AstThree.class))
		{
			removeTouching(AstThree.class);   
            getWorld().removeObject(this);
		}
		else if(isTouching(RightWall.class))
		{
			getWorld().removeObject(this);
		}
    }    
}
your error message raise because you already removed the LaserBeam Object and still want to do something, suppose when the laser hit the astone you want to remove the astone and the laser as well, so the object lasetbeam was removed and still have to execute hitDetectionTwo(); hitDetectionThree(); checkSide(); and make sure you always read the manual/documentation online or offline that provided in greenfoot http://www.greenfoot.org/files/javadoc/
Heneong Heneong

2015/6/11

#
Thanks a lot azazeel, it worked!
You need to login to post a reply.