多个Sprite动作

时间:2011-03-29 21:12:18

标签: objective-c cocos2d-iphone ccsprite

我正在使用Cocos2d引擎,我遇到了一个奇怪的问题。

我有一个精灵。另外,我为该精灵有2个动画。我想在app加载时播放一个动画,然后在调用ccTouchevent后播放。

walkAnim = [CCAnimation animation]; 
dropAnim = [CCAnimation animation]; 
for( int q=1;q<12;q++){
    [walkAnim addFrameWithFilename: [NSString stringWithFormat:@"walkforward_%.2d.png", q]];
    [dropAnim addFrameWithFilename: [NSString stringWithFormat:@"drop_%.2d.png", q]];
}
action = [CCAnimate actionWithAnimation:walkAnim];
action.duration = 2;
id act = [CCRepeatForever actionWithAction:action];
[sprite runAction:act];

所以,我们在这里看到一个动画精灵。

[sprite stopAllActions]; //and here my torture begins

我尝试了许多创造动作的方法:
我试图添加另一个AnimateAction,试图替换当前的动画,但一切都会导致崩溃。

[action setAnimation:dropAnim];

CCAnimate* animat = [[CCAnimate alloc]initWithDuration:30 animation:dropAnim restoreOriginalFrame:YES];

id action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:dropAnim]];
[player1 runAction:action];

崩溃发生在[CCAnimate actionWithAnimation:]

+(id) actionWithAnimation: (CCAnimation*)anim
{    
    return [[[self alloc] initWithAnimation:anim restoreOriginalFrame:YES] autorelease];
}

谢谢!

要从其他方法启动操作,您必须保留操作例如:[操作保留];

1 个答案:

答案 0 :(得分:0)

要从其他方法启动操作,您必须保留操作
例如:[行动保留];然后别忘了发布

-(void)create{
    for( int q=1;q<12;q++){
        [playerWalkAnim addFrameWithFilename: [NSString stringWithFormat:@"walkforward_%.2d.png", q]];
    }
    playerAction = [CCAnimate actionWithAnimation:playerWalkAnim];
    playerAction.duration = 2;

    [playerAction retain];
}

-(void)launch{
    [player1 runAction:playerAction];
}