我使用 cocos2d 2.0,观察到精灵动画切换时崩溃。这是代码
@interface PPActor : CCSprite
{
CCRepeatForever *pirateIdleAnim;
CCSequence *mPirateDashAnim;
CCRepeatForever *mPirateRocketAnim;
}
//情况一:玩第二次就崩溃了
-(void)loadAnim
{
CCAnimate *AnimActions = [CCAnimate actionWithAnimation:animation];
id calFun = [CCCallFunc actionWithTarget:self selector:@selector(dashAnimDone)];
mPirateDashAnim = [CCSequence actions:AnimActions, calFun, nil];
mPirateDashAnim.tag = kTagDashAnim;
}
-(void)runDashAnim
{
[self stopActionByTag: kTagDashAnim];
[self runAction:mPirateDashAnim];
}
//案例2:没有crash,但是显示内存泄漏...(使用retain)
-(void)loadAnim
{
CCAnimate *AnimActions = [CCAnimate actionWithAnimation:animation];
id calFun = [CCCallFunc actionWithTarget:self selector:@selector(dashAnimDone)];
mPirateDashAnim = [[CCSequence actions:AnimActions, calFun, nil] retain];
mPirateDashAnim.tag = kTagDashAnim;
}
-(void)runDashAnim
{
[self stopActionByTag: kTagDashAnim];
[self runAction:mPirateDashAnim];
}
//案例3:再次观察到崩溃
-(void)loadAnim
{
CCAnimate *AnimActions = [CCAnimate actionWithAnimation:animation];
id calFun = [CCCallFunc actionWithTarget:self selector:@selector(dashAnimDone)];
mPirateDashAnim = [CCSequence actions:AnimActions, calFun, nil];
mPirateDashAnim.tag = kTagDashAnim;
}
-(void)runDashAnim
{
[self stopActionByTag: kTagDashAnim];
[self runAction:[mPirateDashAnim copy]];
}
如何克服这种崩溃和内存泄漏?