如何暂停和恢复应用到ccSprite的动作? [英] how to pause and resume action applied to the ccSprite?

查看:99
本文介绍了如何暂停和恢复应用到ccSprite的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个动画

  [[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,但后来游戏得到暂停,如果我尝试再次触发暂停而不恢复游戏崩溃。


$ b $

解决方案

我解决了我的问题

  [[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]];


I have created an animation like

        [[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]];

I want to pause this animation on particular frame and particular trigger. and then again resume animation after particular trigger.

I tried using stopAction and then again runAction. But it stops working after some iteration. and i need to crate animation and action again in order to run that animation again.

I tried using pauseSchedulerAndAction and resumeSchedulerandAction but then game get paused and if i try to trigger pause again without resuming game crashes.

is there anything that will pause the animation on particular frame and resum again from same frame.

解决方案

I solved my problem by creating a animation with 1 frame

[[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]];

i used the above code where i want to pause my animation action. and added following code when i want to resume the animation action.

[[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]];

这篇关于如何暂停和恢复应用到ccSprite的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆