CCAnimation和CCAnimate泄漏?

时间:2011-07-13 14:03:37

标签: cocos2d-iphone

在我的@Monster课程中,有3种不同的步行动画。

“Monster.h”

@interface Monster : CCSprite{
   CCAction *fWalk;
   CCAction *bWalk;
   CCAction *hWalk;
}
@property (nonatomic, retain) CCAction *fWalk;
@property (nonatomic, retain) CCAction *bWalk;
@property (nonatomic, retain) CCAction *hWalk;

“Monster.m”

+ (id) monsterInit...
{
    Monster *sprite = ...// initialization

    NSMutableArray *frameArray = [NSMutableArray array];
    for ( int i = 0; i < 3; i++ ) {
        NSString *fileName = [NSString stringWithFormation:@"%d.png", i];
        [frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
    }
    CCAnimation *walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
    self.fWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];

    [frameArray removeAllObjects];
    for ( int i = 3; i < 6; i++ ) {
        NSString *fileName = [NSString stringWithFormation:@"%d.png", i];
        [frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
    }
    walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
    sprite.bWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];

    [frameArray removeAllObjects];
    for ( int i = 6; i < 9; i++ ) {
        NSString *fileName = [NSString stringWithFormation:@"%d.png", i];
        [frameArray addObject[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:fileName]];
    }
    walk = [CCAnimation animationWithFrames:frameArray delay:0.1f];
    sprite.hWalk = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]];

    return sprite;
}

- (void) dealloc
{
    [fWalk release];
    [bWalk release];
    [hWalk release];

    [super dealloc];
}

当我使用性能工具运行此应用程序时 - 泄漏。仪器显示的语句是“CCAnimation * walk ...”,“self.fWalk ......”,“walk = ...”,“self.bWalk ...”,“walk = ......”,“self .hWalk ..“引发内存泄漏。

我检查了有关CCAnimation和CCAnimate的源代码,它们都是“自动释放”。我不知道为什么会发生这种泄漏。 有什么想法吗?

2 个答案:

答案 0 :(得分:2)

如果您打算稍后使用它们,则必须保留这些操作,因为在使用actionWithAction方法创建时,它们是自动释放的。像这样:

self.fWalk = [[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walk restoreOriginalFrame:NO]] retain];

否则,你的怪物类不拥有动作,当你离开init方法时它们会自动释放。

答案 1 :(得分:1)

好的但是当你将所有动画保存到这样的数组中时:

CCAnimation *anim = [CCAnimation animationWithSpriteFrames:animFrames delay:pDelay];
CCAnimate *animate = [CCAnimate actionWithAnimation:anim];
CCCallFunc *callback = [CCCallFunc actionWithTarget:pTarget selector:pCallBack];
CCSequence *seq = [CCSequence actions:animate, callback , nil];

NSMutableDictionary *animations;

[animations setValue:seq forKey:pName];

- (void)dealloc {
    for (NSString* key in [animations allKeys]) {
        CCAction *action = [animations objectForKey:key];
        [self stopAction:action];
        [action stop];
    }

    [animations removeAllObjects];
    //[[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
}

CCAnimation不会发布......