0

由于“内存压力”,我在 iPhone 4s 上遇到崩溃。我的游戏是这样设置的:

  1. 始终保留在内存中的主要场景精灵表。
  2. 单独的游戏场景关卡从单独的纹理(不是精灵表)加载。

当一个关卡完成并返回主场景时,我希望那些缓存的游戏场景纹理被转储。最终发生的情况是,当您玩 3-4 个级别时,它会因为内存不足而崩溃,因为它在一个级别之后永远不会释放此内存。我不希望在游戏场景的生命周期之后缓存关卡纹理。返回主场景时,需要释放这段内存。

我已经尝试删除所有对记忆没有任何作用的游戏场景子项。我试图寻找一种特定的方法来从缓存中清除我在这个游戏场景中加载的这些纹理。

有什么建议么?

4

2 回答 2

1

Are you using cocos2d v2? You probably have memory leaks since unused textures are removed when necessary. Try profiling your app to see if you have leaks and where they are at.

You also can call these methods yourself at the appropriate time, although I doubt you'd have to:

[[CCTextureCache sharedTextureCache] removeUnusedTextures];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];

But again what you describe sounds more like memory leaks. When your application receives a memory warning the cached data is purged. During this purge the remove unused textures method is called on the texture cache, among other things. If you have 3/4 levels of data still lurking around long after you've exited those scenes, that sounds like a memory leak.

I'm assuming this only happens after visiting multiple scenes and the problem just isn't that your 4th scene is simply trying to load more data than the device can handle.

于 2014-08-05T19:54:06.260 回答
1

您可以删除特定的纹理调用:

在 cocos2D-x v3.x 中:

Director::getInstance()->getTextureCache()->removeTextureForKey(ImageKeyName)

在 cocos2D-x v2.x 中:

CCTextureCache::sharedTextureCache()->removeTextureForKey(ImageKeyName);

其中 ImageKeyName 只是图像的路径(与加载纹理相同)

于 2014-08-05T21:45:46.907 回答