如何在一个ccSprite中运行多个ccAnimation

时间:2013-10-22 11:39:24

标签: cocos2d-iphone

我有一个角色精灵,我必须在其上运行多个ccAnimation,如运行动画和跳转动画。为此,我创建了spritesheet并为其分配帧。这是我的代码:

 [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ch_run_slow.plist"];


    spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"ch_run_slow.png"];
    [self addChild:spriteSheet];

    _character = [CCSprite spriteWithSpriteFrameName:@"Ch_run_slow_12.png"];

    _character.tag=1;


    [spriteSheet addChild:_character];

我的动画功能是:

-(void) characterSlowRun

{
      NSMutableArray *runSlowAnimFrames = [NSMutableArray array];
for (int i=1; i<=12; i++)
{

    [runSlowAnimFrames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
      [NSString stringWithFormat:@"Ch_run_slow_%d.png",i]]];

}


CCAnimation *runSlow = [CCAnimation
                          animationWithSpriteFrames:runSlowAnimFrames delay:0.1f];

runSlowAction=[CCAnimate actionWithAnimation:runSlow];
runSlowAction = [CCRepeatForever actionWithAction:
   [CCAnimate actionWithAnimation:runSlow]];
[_character runAction:runSlowAction];



}

和Jump Action方法是:

-(void) characterJumpSmall
{

[[CCSpriteFrameCache sharedSpriteFrameCache] removeSpriteFramesFromFile:@"ch_run_slow.plist"];


[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"ch_jump_small.plist"];

spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"ch_jump_small.png"];

NSMutableArray *jumpSmallAnimFrames = [NSMutableArray array];
for (int i=1; i<=13; i++)
{

    [jumpSmallAnimFrames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
      [NSString stringWithFormat:@"Ch_jump_small_%d.png",i]]];

}
CCAnimation *jumpSmall = [CCAnimation
                        animationWithSpriteFrames:jumpSmallAnimFrames delay:0.1f];

jumpSmallAction=[CCAnimate actionWithAnimation:jumpSmall];

[_character runAction:jumpSmallAction];

}

在init上调用[self characterSlowRun];并在ccTouchesEnded我使用[_character stopAction:runSlowAction];         [self characterJumpSmall]; 最初runSlow动作工作正常,当它在屏幕上点击它崩溃。跳动不起作用。我所做的?请帮帮我

2 个答案:

答案 0 :(得分:0)

你必须用[_charactar stopAllActions]停止你的walkSlow Action,因为walkSlow无休止地运行。

答案 1 :(得分:0)

Assertion failure in -[CCSprite setTexture:]。就这样?我确信您的错误消息有点长。你也可以查看cocos2d源代码,因为它是开源的,看看哪一行导致了这个断言失败。

无论如何,似乎你的touble只是试图在一个精灵上使用来自不同spritesheets的帧。将您的动画放在一个spritesheet上,它应该可以正常工作。