检查当前是否正在运行某个操作?

Vol*_*ort 5 objective-c cocos2d-iphone

是否可以检查Cocos2d中的CCNode类中是否存在当前正在运行的操作?我想知道a CCMoveBy是否还在运行.

Sol*_*ist 6

您可以[self numberOfRunningActions]在任何CCNode上使用.在你的情况下,听起来你想知道是否有任何动作正在运行,所以事先知道确切的数字并不是什么大问题.


sg7*_*sg7 5

我们可以使用getActionByTag方法和action.tag属性轻松检查是否运行了特定操作.无需引入CCCallFuncN回调或计数numberOfRunningActions.

例.

在我们的应用程序中,重要的是让jumpAction在执行另一次跳转之前完成.为了防止在已经运行的跳转动作期间触发另一次跳转,代码的关键跳转部分受到如下保护:

#define JUMP_ACTION_TAG   1001

-(void)jump {
    // check if the action with tag JUMP_ACTION_TAG is running:
    CCAction *action = [sprite getActionByTag:JUMP_ACTION_TAG]; 

    if(!action) // if action is not running execute the section below:
    {
        // create jumpAction:
        CCJumpBy *jumpAction = [CCJumpBy actionWithDuration:jumpDuration position:ccp(0,0) height:jumpHeight jumps:1];

        // assign tag JUMP_ACTION_TAG to the jumpAction:
        jumpAction.tag = JUMP_ACTION_TAG;

        [sprite runAction:jumpAction];    // run the action
    }
}
Run Code Online (Sandbox Code Playgroud)


Dai*_*air 1

您始终可以添加一个方法来指示该方法何时完成,然后切换一些 BOOL 或类似的内容来指示它没有运行,并添加一个启动方法来切换 BOOL 以指示它已启动:

id actionMove = [CCMoveTo actionWithDuration:actualDuration 
 position:ccp(-target.contentSize.width/2, actualY)];

id actionMoveDone = [CCCallFuncN actionWithTarget:self 
selector:@selector(spriteMoveFinished:)];

id actionMoveStarted = [CCCallFuncN actionWithTarget:self 
selector:@selector(spriteMoveStarted:)];

[target runAction:[CCSequence actions:actionMoveStarted, actionMove, actionMoveDone, nil]];
Run Code Online (Sandbox Code Playgroud)

从这里修改

在两个@selector方法中:

-(void) spriteMoveStarted:(id)sender {
    ccMoveByIsRunning = YES;
}
Run Code Online (Sandbox Code Playgroud)

和:

-(void) spriteMoveFinished:(id)sender {
    ccMoveByIsRunning = NO;
}
Run Code Online (Sandbox Code Playgroud)

其中 ccmoveByIsRunning 是我指的 BOOL。

编辑:正如 xus 所指出的,你实际上不应该这样做,而应该像[self numberOfRunningActions]其他人指出的那样使用。