Whenever I try to remove an object/child with enterframe running I always get null reference error.
In my particular case, the setup is Battlefield contains a lot of Robot:
- A child (Robot) dispatchEvent that it is destroyed
- The parent container receives the event and starts removing the child by removeChild and remove the child from an array of Robots.
- on enterframe, during a loop to move the robots around, sometimes I would get null reference, so I have to call if (robots[i] == null) continue;
How do you safely remove the child without sprinkling if robot is null all over my enterframe?
one idea I have is to have a list of robots to be removed inside the enterframe that checks whether there is a robot to be removed, and if there is, do the removal there instead of the callback on robot exploded event.
Two things I normally do:
1) Have an
activebool in the object getting updated. When you call yourdestroy()function or whatever to cleanup, setactivetofalse. In theupdate()function, make a check at the start. ifactiveisfalse, quit out.2) Have a
removeFromUpdateproperty that’s set to true when you want to delete your object. In your UpdateManager (or whichever calls theupdate()on your objects) do something like this:That pretty much works for me. You can optionally have the object’s
update()function return true if it should be removed if you want to save on a parameter