Reputation: 22042
I used cocos2d 2.0, observed crash when sprite animation switched. Here is code
@interface PPActor : CCSprite
{
CCRepeatForever *pirateIdleAnim;
CCSequence *mPirateDashAnim;
CCRepeatForever *mPirateRocketAnim;
}
//Case 1: Crashed on playing second time
-(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];
}
//Case 2: No crash, but shows memory leaks…(used 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];
}
//Case 3: again crash observed
-(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]];
}
How can I overcome this crash and memory leaks?
Upvotes: 0
Views: 234
Reputation: 64477
Do you always call loadAnim before runDashAnim? Because actions are one-time object. When the action finishes, it releases itself. You have to create the action again.
Please do not follow said "best practices" that recommend retaining actions. It's bad practice because unless you know an action's internal behavior (ie code) there's the risk of creating memory leaks or crashes because not all actions can be reused in that way.
Upvotes: 1