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

2014/7/13

Calling a Method on an Actor Not Currently in the World

ddemarco06 ddemarco06

2014/7/13

#
Looking for any assistance I can find, I am very new to this so any help or advice would be greatly appreciated. I am making a game where when hero collects X number of collectible objects in the world a "Boss" enemy appears, and all the other enemies and objects except for the hero are removed for the world. My hero shoots lasers and I have a method that checks the collision between my lasers and normal enemies and a separate method that checks for collisions between my lasers and the Boss enemy. I have calls to both of these methods in the act method. My problem is when the game begins, the Boss is not yet in the world so there is an error: "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." I have tried a number of different things to get this to work, including moving the methods to the world class, but nothing has seemed to work so far. Any advice that could steer me in the right direction would be appreciated. Thank you!
danpost danpost

2014/7/13

#
You can place the following line at the beginning of the method that errs:
if (getWorld() == null) return;
This will prevent the rest of the method from executing if the actor (Boss object or whatever) is not in the world.
ddemarco06 ddemarco06

2014/7/13

#
@danpost That worked perfectly. Thanks very much!
You need to login to post a reply.