Cocos2D中,iOS的:什么是从缓存中删除精灵正确的方法是什么?(Cocos2d, iOS: w

2019-09-22 12:18发布

I got a GameScene class that is not a singleton instance. Hence I allocate and deallocate it every time the user chooses a new level and keep the "static"/"shared" data in a different singleton class (E.g. GameManager).

I am using ARC. I would like to understand if my approach is correct under the memory management point of view. In other words, is it enough to call "removeUnusedSpriteFrames" at cleanup? Will this remove all sprites in game-art-forLevelOne-hd.plist? Should I do something also with the CCSpriteBatchNode?

-(void) loadGameArtFileForLevelOne  //I do this in a switch but this is to simplify the reading 
{
    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
    [frameCache addSpriteFramesWithFile:@"game-art-forLevelOne-hd.plist"];
    CCSpriteBatchNode * sharedSpriteBatchNode = [CCSpriteBatchNode batchNodeWithFile:@"game-art-forLevelOne-hd.png"];    
 }

//As dealloc is deprecated for ARC code, I prefer to remove unused sprites and texture on cleanup
-(void) cleanup
{
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];     
}

I added a CCLOG call in the dealloc method of CCSpriteBatch node. When I load the GameScene and then go back to the menu I see the log of "remove unused texture" as expected but I don't see any log of the dealloc method of CCSpriteFrameCache. *!! Warning: See edit below - my mistake!!*

EDIT: Sorry, I meant I don't see any log of the dealloc method of CCSpriteBatch

This concerns me slightly as I want to "free" and remove all memory related to the Sprites for the level.

Once I understood this I will then "optimize" this bit (e.g. have one sprite sheet for each class of level - e.g. world 1 - and one sprite sheet for each background).

Any suggestion?

Answer 1:

编辑:

我假设你要添加的CCSpriteBatchNode某个节点,虽然你贴不显示它的代码(你还别说,这在交换机发生,所以我想在一个完全不同的设置)。 如果是这样,它不会被释放,直到你从那里您添加它来删除它。

在这里,也许,澄清是为了。

CCSpriteFrameCache是一个缓存:一块存储的,所有的精灵才能得到随时可以重复使用。

CCSpriteBatchNode是在原则上从缓存中完全无关的Open GL的层提高性能的机制。

我明白,如果你有通过仍然存在于一些层/节点/场景一批节点使用一些精灵,比它不会被视为“未使用”,并且不会被你的清理代码被删除。 其实,这是在使用中。 缓存的精灵将成为“未使用”当您从引用它的所有节点中删除。

至于你的问题:

换句话说,是不是足以叫“removeUnusedSpriteFrames”在清理?

嗯,我认为这取决于你的应用程序有什么可评价为良好清理。 从缓存中删除未使用的精灵肯定是好的; 但你也可以做其他的事情:卸载你加载音频资源; 去除未使用(并且如果需要可以被重新加载)所述场景的部分; 等等

希望这可以帮助。

我没有看到CCSpriteFrameCache的dealloc方法中的任何记录。

CCSpriteFrameCache是单身,所以应当对程序的生命周期存在(它可能是不同的,但我敢打赌,这样的话):

 CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];

所以,我不会在事实缓存没有被删除担心; 重要的是,它的内容被清空。



Answer 2:

你可以使用这个..

 [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];

如果您需要更多然后使用如下。

[[CCDirector sharedDirector] purgeCachedData];
[[CCTextureCache sharedTextureCache] removeAllTextures];
[CCTextureCache purgeSharedTextureCache];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[CCSpriteFrameCache purgeSharedSpriteFrameCache];


文章来源: Cocos2d, iOS: what is the correct way to remove sprites from cache?