Sunday, June 19, 2011

Safely detaching a entity from scene

To safely remove a entity from scene on finish path modifier i recommend use EntityDetachRunnablePoolUpdateHandler.

1) Define variable on main class
     private EntityDetachRunnablePoolUpdateHandler detachPoolHandler =  new EntityDetachRunnablePoolUpdateHandler();
2) Register handler

3) Add to onModifierFinished of LoopEntityModifier detaching of shape with use of EntityDetachRunnablePoolUpdateHandler

new LoopEntityModifier(new IEntityModifierListener() {
            public void onModifierFinished(final IModifier<IEntity> pEntityModifier, final IEntity pEntity) {
                EntityDetachRunnablePoolItem edrpi = detachPoolHandler.obtainPoolItem();
                edrpi.set(entity, scene.getChild(0));
                visible = false;
        }, 0, null, new SequenceEntityModifier(new ScaleModifier(0.2f, 1f, 0.1f)));

When the EntityDetachRunnablePoolUpdateHandler gets his next update, the entity is safely removed.