问题描述
我以这种方式加载一个巨大纹理的动画角色:
I load my animated character with a huge texture this way:
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"MyFile.plist"];
后来,当我不再使用该字符时,我释放内存:
And later, when I am no longer using the character, I release memory using:
[[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"MyFile.plist"];
还有:
[[CCTextureCache sharedTextureCache]removeTextureForKey:@"MyFile.png"];
只是为了确定。
这样做是因为它使用的纹理消耗了大量的内存,所以我确保我处理它,所以我的应用程序不会崩溃。 上面的代码是否正确?
I do that because the texture it uses consumes a lot of memory, so I make sure I dispose of it so my app doesn't crash. Is the above code correct?
无论如何,这里是实际的问题:我最终再次加载我的字符,然后再处理。最后,一对夫妇复苏和杀死后,应用程序崩溃。日志中没有提到它,所以我只能怀疑它是由于内存 - 这就是为什么我认为我没有妥善处理纹理。
Anyway, here is the actual problem: I eventually load my character again, and dispose of it again. Eventually, after a couple revives and kills, the app crashes. The log says nothing about it, so I can just suspect that it was due to memory - that's why I assumed that I wasn't disposing the texture properly.
需要创建和处理字符的纹理。
I do need to create and dispose the character's texture. I can't afford to have it preloaded in the game.
推荐答案
我通常使用强力的方法如下:
I usually use the brute force approach as follows:
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
。有椰子扫描它的阵列,在宏伟的计划的东西不是太昂贵。在你的情况下,在我目前的游戏中,我必须加载和沟。 CCTextureCache有CCLOG允许你见证移除是否真的发生,当你期望它或接近它(autorelease有它的怪癖)。如果没有,你可能保留对纹理的一些句柄,精灵,批处理节点,CCAnimation或动作。
at specific moments in the game flow. It is not too too costly to have coco scan its arrays, in the grand scheme of things. As in your case, in my current game i must load and ditch. CCTextureCache has CCLOG'ing that permit you to witness whether the removal actually occurs when you expect it, or close to it (autorelease has its quirks). If not, you are probably retaining some handle to the texture somewhere, a sprite, batch node, an CCAnimation, or an action.
这篇关于释放所有纹理内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!