我正在編寫一個應用程序,您可以按下不同的按鈕並且角色會變成動畫。問題是我有很多圖像,所以我需要爲每個動畫使用一個紋理。因此我需要發佈精靈表和框架現金,但它似乎沒有工作。內存分配越來越多,直到應用程序崩潰。下面是代碼:多個動畫和紋理與Cocos2d - 如何從內存中刪除紋理?
// **** 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];
我嘗試釋放內存這樣的:
[CCTextureCache sharedTextureCache] removeAllTextures]。 [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
謝謝!你有一個例子(聲明紋理,去除紋理和所有參考)? – 2010-10-26 08:00:50