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

2016/11/4

Please help with 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.

powderbanana powderbanana

2016/11/4

#
This code makes a shooting game which shoots bullets to blow balloons. However, when I shot the balloons, It keeps saying following strings : java.lang.IllegalStateException: Actor not in the 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:695) at greenfoot.Actor.getY(Actor.java:170) at Balloon.pointAtObject(Balloon.java:35) at Balloon.act(Balloon.java:30) 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) And the code is : balloon :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Balloon here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Balloon extends Actor
{
    /**
     * Act - do whatever the Balloon wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    
    private int counter;
    public void act() 
    {           
        Actor b = getOneIntersectingObject(bullet.class);  
        Actor turret1 = (Actor)getWorld().getObjects(turret1.class).get(0);
        
       if(b != null)  
       {  
           getWorld().removeObject(b);   
           getWorld().removeObject(this);
       }
       

       pointAtObject(turret1);
       move(1);
    }
    
    public void pointAtObject(Actor o) {
        setRotation((int)(180*Math.atan2(o.getY()-getY(),o.getX()-getX())/Math.PI));
    }
}
bullet :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class bullet extends Actor
{
    /**
     * Act - do whatever the bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
   
    private int speed = 15;

    public void act()
    {
        move(speed);
        
        if(this.getX() >= getWorld().getWidth() - 3 || this.getX() < 3 || this.getY() >= getWorld().getHeight() - 3 || this.getY() < 3) {
            getWorld().removeObject(this);
        }        
    }
}
turret1 :
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class turret1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class turret1 extends Actor
{
    
    public void act() 
    {
        MouseInfo m = Greenfoot.getMouseInfo();  
        if(m != null)
            turnTowards(m);
        if(Greenfoot.mouseClicked(null)){
            int x = m.getX();
            int y = m.getY();
            shoot();
        }    
        
       if (Greenfoot.getRandomNumber(200) == 150)
       {
            int x = Greenfoot.getRandomNumber(1024);
            int y = Greenfoot.getRandomNumber(768);
            
            if(!(x > 402 && x < 522 && y > 309 && y < 509)) {
                getWorld().addObject(new Balloon(), x, y);
            } else {
                x = Greenfoot.getRandomNumber(1024); 
                y = Greenfoot.getRandomNumber(768);
            }
       }
    }
    
    public void shoot()
    {
        // the following two statements were added to the code
        MouseInfo mouse = Greenfoot.getMouseInfo();
        int x = mouse.getX();
        int y = mouse.getY();
        GreenfootSound gun = new GreenfootSound("shotgun.wav");

        if (mouse == null) return;
        
        bullet bullet = new bullet();
        getWorld().addObject(bullet, getX(), getY());
        gun.play();
        bullet.turnTowards(x, y);
    }
    
    public void turnTowards (MouseInfo mi)  
    {  
        turnTowards(mi.getX(), mi.getY());  
    }  

    public void turnTowards (int x, int y)  
    {  
        double dx = x - getX();  
        double dy = y - getY();  
        double angle = Math.atan2(dy,dx)*180.0/(Math.PI);
        setRotation( (int)angle + 180 );  
    }
}
Please help me as fast as you can. Thank you
danpost danpost

2016/11/4

#
After line 26 in the Balloon class (where you are removing the balloon from the world), add a 'return;' statement so execution of further statements in the method are not attempted.
You need to login to post a reply.