Sprite removal

Problem

How do I remove a sprite and make sure it doesn't stay in the memory?

Solution

private void removeSprite(Sprite s) {
        if ( s == null) {
            return;
        }

        nmys.remove(s);
        nmys_r.remove(s);

        final Sprite s2 = s;
        runOnUpdateThread(new Runnable() {
            @Override
            public void run() {
                    /* Now it is save to remove the entity! */
                    mEngine.getScene().detachChild(s2);
                    BufferObjectManager.getActiveInstance().unloadBufferObject(s2.getVertexBuffer());
            }
        });

    }

This code is written with the deprecated functions, if you used Mercurial to get AndEngine, you can write

private void removeSprite(Sprite s) {
        if ( s == null) {
            return;
        }

        nmys.remove(s);
        nmys_r.remove(s);

        runOnUpdateThread(new Runnable() {
            @Override
            public void run() {
                    /* Now it is save to remove the entity! */
                    mEngine.getScene().getTopLayer().removeEntity(s);
                    BufferObjectManager.getActiveInstance().unloadBufferObject(s.getVertexBuffer());
            }
        });

    }
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License