我正在开发一款游戏,可以在触摸的场景中显示不同的精灵。每个场景都有一组(约10个精灵),这些场景会根据用户操作显示和删除。当用户导航到下一个场景时,我加载该场景的精灵,并从缓存中删除第一个场景的精灵。我注意到场景更改时发生少量内存泄漏,并且引脚指向在1s场景中创建的TiledTextureRegion变量。

我尝试了sprite.reset()调用,但没有帮助。我正在从场景中删除精灵。这是remove sprite代码的样子:

    private void removeSprite(final AnimatedSprite sprite) {
    final PhysicsConnector facePhysicsConnector = this.mPhysicsWorld.getPhysicsConnectorManager().findPhysicsConnectorByShape(sprite);

    this.mPhysicsWorld.unregisterPhysicsConnector(facePhysicsConnector);
    this.mPhysicsWorld.destroyBody(facePhysicsConnector.getBody());

    this.mScene.unregisterTouchArea(sprite);
    this.mScene.detachChild(sprite);

    System.gc();
}


但是看起来它没有清除与sprite相关联的TiledTextureRegion对象。因为,我在应用程序中会有很多不同的场景,所以我担心内存泄漏会加起来并引起问题。
任何想法或建议将不胜感激。
谢谢!!

最佳答案

您无法清除TiledTextureRegion,但可以使用BitmapTextureAtlas清除BitmapTextureAtlas.unload();

例如:

BitmapTextureAtlas Texture1 = new BitmapTextureAtlas(null, 1024, 1024, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
mEngine.getTextureManager().loadTexture(Texture7);
ITextureRegion example = BitmapTextureAtlasTextureRegionFactory.createFromAsset(Texture1, this, "picture.png", 0, 0);


然后,当您需要时:

Texture1.unload();


但是可能不需要清除内存,因为您将再次使用TiledTextureRegion。您将重新创建TiledTextureRegion,这将减慢您的应用程序的速度。

10-08 17:52