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

2012/12/18

What is wrong with my programming?

hades530 hades530

2012/12/18

#
I keep getting the following lines: 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:663) at greenfoot.Actor.getOneIntersectingObject(Actor.java:912) at Animal.canSee(Animal.java:93) at Rocket.Eat(Rocket.java:38) at Rocket.act(Rocket.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:565) at greenfoot.core.Simulation.runOneLoop(Simulation.java:523) at greenfoot.core.Simulation.runContent(Simulation.java:213) at greenfoot.core.Simulation.run(Simulation.java:203)
hades530 hades530

2012/12/18

#
My code for rocket is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Rocket here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Rocket extends Animal
{
    /**
     * 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() 
    {
        move();
        disappear();
        Eat();
    }    
    
    /**
     * The rockets disappear at world edge
     */
    public void disappear()
    {
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
        }
    }
    
    /**
     * Gets rid of the rat
     */
    public void Eat()
    {
        if (canSee(Rat.class))
        {
            eat(Rat.class);
        }
    }
}
hades530 hades530

2012/12/18

#
and my code for the actor that is shooting the rocket is: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bazooka here. * * @author (your name) * @version (a version number or a date) */ public class Bazooka extends Animal { int shoot; /** * Act - do whatever the Bazooka wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Control(); fire(); } /** * Moves the bazooka man */ public void Control() { int speed = 5; if(Greenfoot.isKeyDown("up")) { setLocation(getX(), getY() - speed); } if(Greenfoot.isKeyDown("down")) { setLocation(getX(), getY() + speed); } } /** * The Bazooka shots a rocket. */ public void fire() { shoot = shoot + 1; if(Greenfoot.isKeyDown("space") && shoot > 15) { Rocket rocket = new Rocket(); getWorld().addObject(rocket, getX(), getY()); rocket.setRotation(getRotation()); shoot = 0; } } }
Hawx_ Hawx_

2012/12/18

#
Make sure that the rocket is in the world , check in the World class. Secondly , if it is firing , it might be worth using the getKey() command so it would look like this:
if ("space".equals(Greenfoot.getKey()))  //etc...
          
Hawx_ Hawx_

2012/12/18

#
Not sure if this is any help.....
vonmeth vonmeth

2012/12/18

#
    public void act()   
    {  
        move();  
        disappear();  
        Eat();  
    }      
Rearrange it so it is like so:
    public void act()   
    {  
        move(); 
        Eat();
        disappear();  
    }      
You are removing your Rocket, then trying to use it's position with "canSee(Rat.class)", but it is no longer in the world, so you are getting that error. Rearranging like above, if the Rocket does get removed, it will be the last thing it attempts to do.
tylers tylers

2012/12/18

#
have not tried it but this may work:
 public void disappear()  
    {  
        if (atWorldEdge())  
        {  
            getWorld().removeObject(this);  
            return;
        }  
    }  
hades530 hades530

2012/12/18

#
vonmeth, thank you so much, it worked! thanks Hawx and tylers for your help too!
danpost danpost

2012/12/18

#
@tylers, putting a 'return' statement after the statement that removes 'this' object from the world is only useful if there is more code in the method currently being executed. If no rearrangement was done to the method calls in the 'act' method, the 'Eat' method would begin execution after the return from 'disappear' no matter what happens in the 'disappear' method (or how it is exited). The other option, other than rearrangement of the method calls, is to insert the following statement between the two calls:
if(getWorld()==null)return;
tylers tylers

2012/12/18

#
oh just realised that, thanks :D
You need to login to post a reply.