开发者

Multiple animations and textures with Cocos2d - How to remove texture from memory?

I am writing an application where you push different buttons and a character gets animated. The thing is that I have many images, so I need to use one texture for each animation. Therefore I need to release sprite sheet and frame cash, but it does not seem to be working. Memory gets allocated more and more until the app crashes. Here is the code:

// **** DEFINE THE ANIMATION - EATING 1: ****

// Create a sprite sheet with all the images
CCSpriteSheet *spriteSheet = [CCSpriteSheet spriteSheetWithFile:@"Eating4.png"];

// This loads an image of the same name (but ending in png), and goes through the
// plist to add definitions of each frame to the cache.
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Eating4.plist"];    

[self addChild:spriteSheet];
///[self addChild:spriteSheet2];

// Load up the frames of our animation
NSMutableArray *eating1AnimFrames = [NSMutableArray array];
for(int i = 0; i <= 50; ++i) {
    if (i<=9){
        [eating1AnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Eating4_000%d.png", i]]];
    }
    else if (i>9) {
        [eating1AnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Eating4_00%d.png", i]]];
    }
}
CCAnimation *eatingAnim = [CCAnimation animationWithName:@"eating" delay:0.05f frames:eating1AnimFrames];

// Create a sprite for the mouse
CGSize winSize = [CCDirector sharedDirector].winSize;
self.mouse = [CCSprite spriteWithSpriteFrameName:@"Eating4_0000.png"];  
_mouse.position = ccp(winSize.width/2+20, winSize.height/2);
// Adjust the size of the Sprite:
[_mouse setScaleX: 1];
[_mouse setScaleY: 1];

//self.eatingAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:eatingAnim restoreOriginalFrame:NO]];
self.eatingAction = [CCAnimate actionWithAnimation:eatingAnim 开发者_如何转开发restoreOriginalFrame:NO];
[spriteSheet addChild:_mouse];

[_mouse runAction:_eatingAction];

I try to release memory like this:

[[CCTextureCache sharedTextureCache] removeAllTextures]; [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];


[[CCTextureCache sharedTextureCache] removeAllTextures];

You don't want to do this! This removes all textures from memory, then reloads all of them that are still in use or the next time they are used. This causes a significant overhead and it's generally bad for your game's performance.

Instead, remove only the texture you need to remove by calling:

[[CCTextureCache sharedTextureCache] removeTexture:tex];

For this to work, you will also have to remove (release) all references to the animation from the Cocos2D node hierarchy.


Calling removeUnusedTextures would be a better choice for clearing up memory used by unused textures:

[[CCTextureCache sharedTextureCache] removeUnusedTextures];

Or you can ask use CCDirector::purgeCachedData that does the same thing internally:

[[CCDirector sharedDirector] purgeCachedData];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜