如何暂停和恢复应用于ccSprite的操作?

时间:2011-07-11 18:08:15

标签: iphone cocos2d-iphone

我创建了一个像

这样的动画
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     @"Actor.plist"];
    spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Actor.pvr.ccz"];
    [self addChild:spriteSheet];
    NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 8; ++i) {
        [walkAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"clip000%d.png", i]]];
    }
    CCAnimation *walkAnim = [CCAnimation 
                             animationWithFrames:walkAnimFrames delay:0.07f];
    playWalkSound = [CCCallFuncN actionWithTarget:self selector:@selector(PlayRunningSound:)];

    walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
    [actor runAction:[CCSpawn actions:walkAction, nil]];

我想在特定帧和特定触发器上暂停此动画。然后在特定触发后再次恢复动画。

我尝试使用stopAction然后再次运行runAction。但它经过一些迭代后停止工作。我需要再次创建动画和动作才能再次运行该动画。

我尝试使用pauseSchedulerAndAction和resumeSchedulerandAction然后游戏暂停,如果我尝试再次触发暂停而不恢复游戏崩溃。

是否有任何东西会暂停特定帧上的动画并从同一帧重新开始。

1 个答案:

答案 0 :(得分:1)

我通过创建1帧

的动画解决了我的问题
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
 @"Actor.plist"];
spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Actor.pvr.ccz"];
[self addChild:spriteSheet];
NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <2; ++i) {
    [walkAnimFrames addObject:
     [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
      [NSString stringWithFormat:@"clip000%d.png", i]]];
}
CCAnimation *walkAnim = [CCAnimation 
                         animationWithFrames:walkAnimFrames delay:0.07f];
playWalkSound = [CCCallFuncN actionWithTarget:self selector:@selector(PlayRunningSound:)];

walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
[actor runAction:[CCSpawn actions:walkAction, nil]];

我使用上面的代码,我想暂停我的动画动作。当我想恢复动画动作时添加以下代码。

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
     @"Actor.plist"];
    spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"Actor.pvr.ccz"];
    [self addChild:spriteSheet];
    NSMutableArray *walkAnimFrames = [NSMutableArray array];
    for(int i = 1; i <= 8; ++i) {
        [walkAnimFrames addObject:
         [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
          [NSString stringWithFormat:@"clip000%d.png", i]]];
    }
    CCAnimation *walkAnim = [CCAnimation 
                             animationWithFrames:walkAnimFrames delay:0.07f];
    playWalkSound = [CCCallFuncN actionWithTarget:self selector:@selector(PlayRunningSound:)];

    walkAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];
    [actor runAction:[CCSpawn actions:walkAction, nil]];