Handling the removal of an entity from the game
Detaching entities within an update handler can occasionally throw an IndexOutOfBoundsException
exception, because the entity is removed in the middle of an engine update. To avoid that exception, we create a Runnable
parameter that is run last on the update thread, after all other updating has occurred. In this recipe, we will safely remove an entity from the game by using the BaseGameActivity class' runOnUpdateThread()
method.
Getting ready...
Create a new class named HandlingRemovalOfEntityActivity
that extends BaseGameActivity
. We will use this class to learn how to safely remove an entity from an update handler.
How to do it...
Follow these steps to see how we can remove an entity from its parent without throwing an exception:
Insert the following definitions into the
HandlingRemovalOfEntityActivity
class:public static int cameraWidth = 800; public static int cameraHeight = 480; public Scene mScene; public Rectangle spinningRect; public float...