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

2013/1/11

somehow my codes don't work?

hades530 hades530

2013/1/11

#
so i have been trying to remove the Rocket and the Rat at the same time when they come into contact with one another which works fine. However, I also tried to remove the Rocket when it reaches world edge and when the rocket reaches world edge, it gives me this: 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. is there a way to get the Rocket removed upon contact with the Rat AND get the "uncontacted" Rockets to be get removed at world edge
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);
            getWorld().removeObject(this);
        }
    }
}
danpost danpost

2013/1/11

#
After 'disappear' executes, you are calling 'Eat' which requires the objects location in the world. If the object is removed in 'disappear', you will get that error. Easy fix: put 'Eat' before 'disappear'.
hades530 hades530

2013/1/11

#
Now it stops when it comes to contact with the Rat and gives me a message: 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.
danpost danpost

2013/1/11

#
Sorry, I missed the fact that you could remove 'this' from either method. Modify the 'act' method to the following:
public void act()
{
    move();
    Eat();
    if (getWorld() != null) disappear();
}
// OR
public void act()
{
    move();
    disappear();
    if (getWorld() != null) Eat();
}
hades530 hades530

2013/1/11

#
Thank you so much! It works perfectly now!
Snake12163 Snake12163

2013/1/22

#
can someone please help me with this code; if(atWorldEdge()) { turn(3) } why doesn't it work anymore?
danpost danpost

2013/1/22

#
We know that the code supplied is alright (except for the missing semi-colon at the end of the 'turn' statement). You will have to supply more information and code to solve your problem. Code to supply: the 'atWorldEdge' method; specifying what class it is in the declaration statement of the class which the calling statement (supplied above) is located the entire method that the code supplied above is in the trace from the act method to the method that the supplied code is in (i.e. act>moveAndTurn>move; method-to-method)
You need to login to post a reply.