【问题标题】:Warning: Received memory warning. Level=1 & 2警告:收到内存警告。等级=1 & 2
【发布时间】:2011-04-14 05:08:36
【问题描述】:

我正在制作崩溃的应用程序,并在控制台中给出上述警告。

有两个精灵。

  Train=[CCSprite spriteWithFile:@"train.png"];
    [self addChild:train z:0 tag:1];

  AlphaImage=[CCSprite node];
    AlphaImage.position=ccp(245,155);
    [Train addChild:AlphaImage z:1 tag:2];

在其他方法中,我使用动画初始化 AlphaImage。

CCSpriteFrameCache *frameCache1 =[CCSpriteFrameCache sharedSpriteFrameCache];   
[frameCache1    addSpriteFramesWithFile:plist1];                                
CCSpriteBatchNode *danceSheet1 = [CCSpriteBatchNode batchNodeWithFile:png1];    
[self addChild:danceSheet1];                                

NSMutableArray *animFrames1 = [NSMutableArray arrayWithCapacity:frame[x]];      
for(int i = 1; i < frame[x]+1; i++) {
    NSString *namef1=[NSString stringWithFormat:@"%@%i.png",alpha1,i];              
    CCSpriteFrame *frame1 = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:namef1];   


    [animFrames1 addObject:frame1];
}

CCAnimation *anim1 = [CCAnimation animationWithFrames:animFrames1 delay:0.3f]; 
CCAnimate *animN1 = [CCAnimate actionWithAnimation:anim1];                      
CCRepeatForever *repeat1 = [CCRepeatForever actionWithAction:animN1];                   
[AlphaImage runAction:repeat1];     

我已经使用 zwoptex 创建了 plist 和纹理图集。
随着火车的行驶和返回,用于动画更改的 plist 和纹理图集。但经过 4 或 5 次应用程序崩溃。在新动画出现在 AlphaImage 之前,我还处理了所有帧和纹理。我使用了这个:

[CCSpriteFrameCache purgeSharedSpriteFrameCache];  
[CCTextureCache purgeSharedTextureCache];   

我正在为应用程序使用高清图像。我浏览了许多文档,他们提出了这些建议:

[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFrames];
[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];

但在控制台中,它没有显示任何关于使用这些行删除框架的信息。 有什么我做错了吗?

【问题讨论】:

    标签: memory-management cocos2d-iphone didreceivememorywarning


    【解决方案1】:

    你在哪里运行[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames]; 这个语句使用 NSObject 的 performSelectorInBackground 方法运行它的后台线程。

    【讨论】:

    • 在我的 AllReset 方法中,我有动画代码(上面的代码),在此之前我调用了包含此代码的 removeUnused 方法。
    • 请参阅此页面以获取 performSelectorInBackground - blog.datispars.com/…
    • performSelectorInBackground 没有响应选择器,它正在循环中。
    【解决方案2】:

    尝试在动画结束后释放一个数组。

    //可能是这个原因。

    [animFrames1 release];
    animFrames1 = nil;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 2011-09-12
      • 2011-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多