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

2016/11/2

Checking if there is an object

LimeLava LimeLava

2016/11/2

#
I have one object shooting another object and there are multiple of the item i am shooting at and when the bullet hits the object that i am shooting at I am shooting at it removes it but i get an error about there not being an object for me to do stuff with even though there are multiple of that object in the world. Here is the code for the object i want to remove (Shooter is the bullet)
public class Apple extends Actor
{
    private int numApples = 3;
    /**
     * Act - do whatever the Apple wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        remove();
        move(4);
        edge();
        randomTurn();
    }    
    public void remove()
    {
        
        if (isTouching( Camel.class))
        {
            getWorld().removeObject(this);
        }
        if (isTouching (Shooter.class))
        {
            getWorld().removeObject(this);
        }
    }
If you need the code for any of the other objects let me know. I get an error for line 18,30, and 37.
danpost danpost

2016/11/2

#
The act method executes once for each actor of it type in the world each act cycle. That means if you have ten Apple objects in the world, the act method of the Apple class will execute ten times, one time for each of those apples. The act method will execute to its end each time. If you remove 'this' apple, the apple that the act method is currently being executed for (see line 20), then the next time you try to execute any method that requires 'this' actor be in the world, you will get an error. Line 22 requires the actor be in the world (as well as line 24). You can combine the two if structures since they both do the same thing if their conditions are true:
if (isTouching(Camel.class) || isTouching(Shooter.class))
{
    getWorld().removeObject(this);
}
to avoid the error.
LimeLava LimeLava

2016/11/4

#
I now have a similar error but i am not sure how to fix this section,
public void edge()
    {
        if (getX()>= 595)
        {
            setLocation(5,getY());
        }
        if (getX()<= 2)
        {
            setLocation(695,getY());
        }
        if (getY()>=395)
        {
            setLocation(getX(),3);
        }
        if (getY()<= 2)
        {
            setLocation(getX(),395);
        }
    }
This is for the apples and I get the error when I kill one of the apples.
danpost danpost

2016/11/4

#
LimeLava wrote...
I now have a similar error but i am not sure how to fix this section < Code Omitted > This is for the apples and I get the error when I kill one of the apples.
Move the call to the remove method to the end of the act method.
LimeLava LimeLava

2016/11/7

#
I did that but the same thing seems to happen. This is the code for the object that i have screen warping and i get the errors for when i eat it.
 private int numApples = 3;
    /**
     * Act - do whatever the Apple wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        remove();
        move(4);
        randomTurn();
        edge();
    }    
    public void remove()
    {
        
        if (isTouching(Camel.class) || isTouching(Shooter.class))
        {
         getWorld().removeObject(this);
        }
    }
    public void randomTurn()
    {
         if (Greenfoot.getRandomNumber(100) < 10){
            turn(Greenfoot.getRandomNumber(90)-45);
        }
         if (Greenfoot.getRandomNumber(100) < 2){
            turnTowards(250,250);
        }
    }
    public void edge()
    {
        if (getX()>= 595)
        {
            setLocation(5,getY());
        }
        if (getX()<= 2)
        {
            setLocation(695,getY());
        }
        if (getY()>=395)
        {
            setLocation(getX(),3);
        }
        if (getY()<= 2)
        {
            setLocation(getX(),395);
        }
    }
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. for lines 42 and 21.
LimeLava LimeLava

2016/11/7

#
Never mind
You need to login to post a reply.