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

2013/12/12

Error message keeps coming up

Affan Affan

2013/12/12

#
I carry on getting an error message telling me that I am trying to reference an object that doesn't when my shot reaches the end of the world:
      public void deleteIfHit()
    {
        if(atWorldEdge())
        {
           destroy(Shot.class);
        }
        
        if(canSee(asteroido.class))
        {
            destroy(asteroido.class);
            destroy(Shot.class);
        }
    }
Please can you explain to me why the error message keeps coming up when my shot touches the edge of the world, It destroys itself and the asteroid fine.
Gevater_Tod4711 Gevater_Tod4711

2013/12/12

#
When your object touches the worlds edge it get's removed. After that you execute the can see method. To execute this method your object needs to be in the world to check whether your object is touching the asteroid. So you get the error message. You could try adding a return statement after line 5 or just check whether the object is still in the world (if (getWorld() != null) before executing the canSee method.
Affan Affan

2013/12/12

#
Gevater_Tod4711 wrote...
When your object touches the worlds edge it get's removed. After that you execute the can see method. To execute this method your object needs to be in the world to check whether your object is touching the asteroid. So you get the error message. You could try adding a return statement after line 5 or just check whether the object is still in the world (if (getWorld() != null) before executing the canSee method.
Thanks for that. It solved my problem, clear explaination. If there was some kind of karma system I would give you some.
You need to login to post a reply.