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

2017/4/26

what is the error

Ozan31 Ozan31

2017/4/26

#
Hi, i was wondering where the error is within my code because an error message pops up whenever there is a collision.
   public void act() 
    {
        
        move();
        eat();
        if (isAtEdge()) getWorld().removeObject(this);
    }
    public void move()
     {
       setRotation(270);
       move(8); 
    }
     public void eat()
     {
       Actor helicopter = getOneIntersectingObject(Helicopter.class);
       if(helicopter != null)
       {
           World myWorld = getWorld();
           myWorld.removeObject(helicopter);
           myWorld.removeObject(this);
        }
}
danpost danpost

2017/4/26

#
When the 'eat' method remove 'this' actor from the world, the actor (1) no longer has a world and (2) no longer has any determinable coordinates. Therefore, when the next line in the act method is executed, 'isAtEdge', which needs the coordinates of the actor, fails. You can get around this by making sure the actor is in the world before calling 'isAtEdge':
if (getWorld() != null && isAtEdge()) ...
Ozan31 Ozan31

2017/4/26

#
This code has fixed my previous problem, but i have just noticed that the actor is no longer removed when it reaches the edge of the world.
public void act() 
    {
        
        move();
        eat();
        if (getWorld() != null && isAtEdge());
    }
danpost danpost

2017/4/26

#
Ozan31 wrote...
This code has fixed my previous problem, but i have just noticed that the actor is no longer removed when it reaches the edge of the world.
The elipses, '...', in the code I supplied indicate that more goes there. Complete the line as in your line 6 above.
You need to login to post a reply.