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

2016/4/25

Hair-pulling error, please help?

thesaltyhobbit thesaltyhobbit

2016/4/25

#
I keep getting 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. This happens at random, and it's really stressing me out.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class rocks here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class rocks extends Actor
{
    /**
     * Act - do whatever the rocks wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        setLocation(getX(),getY() + 2);        
        checkCollision();
        if (getY() == 599)
        {
            getWorld().removeObject(this);
        }
    }    
    public void checkCollision()
    {
       if(isTouching(Xwing.class))
        {            
            Space space= (Space)getWorld();
            space.takeHealth(-20);
            getWorld().removeObject(this);
        } 
    }
}
danpost danpost

2016/4/25

#
Change line 20 to:
if (getWorld() != null && getY() == 599)
Reason: if the actor gets removed from the world in the call to 'checkCollision' (line 19), then you cannot get an x-coordinate afterwards (line 20).
thesaltyhobbit thesaltyhobbit

2016/4/25

#
Thanks!
You need to login to post a reply.