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

2013/3/8

Simple "if" statement causing problems.

sqwuckies sqwuckies

2013/3/8

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Rocket extends Actor
{
    private Actor actor;
    
    public Rocket()
    {
        setRotation(270);
    }

    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveRocket();
        checkEdge();
        seePlane();
    }    
    
    public void moveRocket()
    {
        move(10);
    }
    
    public void checkEdge()
    {
        if (getY() <= 0)
        {
            getWorld().removeObject(this);
        }
    }
    
    public void seePlane()
    {
        if (getOneIntersectingObject(Plane.class) != null)
        {          
            Greenfoot.playSound("Explosion.mp3");
         }
    }
}
I am trying to make a game in which there is a rocket launcher on the ground, and it shoots rockets at a plane that flies back and forth at the top of the screen. Every time I shoot a rocket, even when it doesn't come in contact with the plane, I get this error: 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. :
if (getOneIntersectingObject(Plane.class) != null)
I don't understand. I've used this exact statement many times before and it's always worked fine. Any suggestions?
erdelf erdelf

2013/3/8

#
maybe you should change the order of the method calls in the act method. put the checkEdge call in the end of the act method
davmac davmac

2013/3/8

#
See this. It's not the "if" that's causing the problem, it's the condition "getOneIntersectingObject(Plane.class) != null". You can't call getOneIntersectingObject from an actor that's not in the world. Your checkEdge method can remove the plane from the world, so that when seePlane is called, you get the exception.
You need to login to post a reply.