我是cocos2d的新手,面临记忆问题 我得到记忆警告等级1& 2.
我的游戏中有两个场景。
在第一个场景(mainmenu)中我有按钮(点击它会用第二个替换场景,即(StartMoving))
在StartMoving.h文件中我有初始化的精灵,数组(我在dealloc方法中取消分配)
在我的StartMoving的init方法中,我初始化了背景Image。(在dealloc方法中重新定义)并转到Allreset方法。
这是我的Allreset方法的代码。
- (无效)AllReset
{
而(X == 5)
{
X = 0;
[[CCDirector sharedDirector] replaceScene:[mainmenu scene]]; //当X = 5
}
CCLOG(@"%@: %@",NSStringFromSelector(_cmd),self);
CCSprite *tra=[CCSprite spriteWithFile:@"tra.png"];
tra.position =ccp(800,115);
[self addChild:tra z:0 tag:2];
[tra1 insertObject:tra atIndex:0]; //tra1 is NSMutablearray which is realeased in dealloc
NSLog(@"tra is loaded....");
//[[SimpleAudioEngine sharedEngine] playEffect:@"police.wav"];
NSArray *name14 =[NSArray arrayWithObjects:@"app1.plist",@"app2.plist",@"app3.plist",@"app4.plist",@"app5.plist", nil];
NSArray *name24 =[NSArray arrayWithObjects:@"app1.png",@"app2.png",@"app3.png", @"app4.png",@"app5.png",nil];
NSArray *name34 =[NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",nil];
NSString *file14=[name14 objectAtIndex:x];
NSString *file24=[name24 objectAtIndex:x];
NSString *file34=[name34 objectAtIndex:x];
int frame[]={3, 7, 11, 4, 5};
float Fdelay[]={0.15, 0.2, 0.3, 0.1,0.5};
CCSpriteFrameCache *frameCache4 =[CCSpriteFrameCache sharedSpriteFrameCache];
[frameCache4 addSpriteFramesWithFile:file14];
CCSpriteBatchNode *danceSheet4 = [CCSpriteBatchNode batchNodeWithFile:file24];
[self addChild:danceSheet4];
CCSprite *sprite4 = [CCSprite node];
sprite4.position = ccp(395,155);
[tra addChild:sprite4 z:1 tag:14];
NSMutableArray *animFrames4 = [NSMutableArray arrayWithCapacity:frame[x]];
for(int i = 1; i <= frame[x]; i++) {
NSString *namef4=[NSString stringWithFormat:@"%@%i.png",file34,i];
CCSpriteFrame *frame4 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:namef4];
[animFrames4 addObject:frame4];
}
CCAnimation *anim4 = [CCAnimation animationWithFrames:animFrames4 delay:Fdelay[x]];
CCAnimate *animN4 = [CCAnimate actionWithAnimation:anim4];
CCRepeatForever *repeat4 = [CCRepeatForever actionWithAction:animN4];
[sprite4 runAction:repeat4];
id move= [CCMoveTo actionWithDuration:5.5f position:CGPointMake(240,115)];
id easeout=[CCEaseOut actionWithAction:move rate:1.5f];
id menudisp=[CCCallFuncN actionWithTarget:self selector:@selector(menudisplay:)];
[train1 runAction:[CCSequence actions:easeout,menudisp, nil]];
}
我这里只显示了一个动画,但同一个精灵上有7个动画。
当tra在给定点停止时,将显示菜单标签。当用户点击标签时,它开始朝同一方向移动。走出屏幕。一旦整个精灵走出屏幕,就会调用一个方法来清理tra的记忆(我想只有tra的孩子才会被清理。如果我错了,请纠正我。)
清理内存后,它将再次使用增加的x值调用Allreset方法。
` - (无效)Incrementingx
{
X ++;
[self Allreset];
}
我还在appdelegate.m的applicationDidReceiveMemoryWarning方法中使用了以下内容
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
// [[CCDirector sharedDirector] purgeCachedData]; //注意这是评论
应用程序在模拟器上运行完全正常但在设备上:(
请帮帮我:(
答案 0 :(得分:4)
在我使用大量精灵表和精灵表动画时,我遇到了类似的问题。使用简单的动画。同时调用不同的场景或在场景之间切换。这些都是我所做的。
//IF you have particular spritesheets to be removed! Don't use these if you haven't any [[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"ufoRotateThird2.plist"]; [[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"ufoRotateSecond1.plist"]; [[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"explode.plist"]; [[CCSpriteFrameCache sharedSpriteFrameCache]removeSpriteFramesFromFile:@"explodeR.plist"]; //Use these [[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames]; [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; //Use these [[CCTextureCache sharedTextureCache] removeUnusedTextures]; [[CCTextureCache sharedTextureCache] removeAllTextures]; [[CCDirector sharedDirector] purgeCachedData]; //Try out and use it. Not compulsory [self removeAllChildrenWithCleanup: YES];
答案 1 :(得分:1)
之前我遇到过这个记忆警告。我只是在iOS设备上,而不是在模拟器上,我发现它是因为运行背景太多了(iOS 4.x)。只需关闭这些应用程序,警告就会消失。
答案 2 :(得分:1)
而是删除dealloc.Better放入onExit。因为当你想在下一个场景中添加精灵帧时,dealloc有时会引起问题。
-(void) onExit{
[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
[super onExit];
}