开发者

Check if an action is currently running?

Is it possible to check if there are actions currently running in a CCNode class in Cocos2d? I'd like to know if a CCMoveBy开发者_C百科 is still running or not.


You can use [self numberOfRunningActions] on any CCNode. In your case, it sounds like you want to know if there are simply any actions running or not, so it's not a big deal to know the exact number beforehand.


We can easily check if specific actions run by using getActionByTag method and action.tag property. There is no need to introduce the CCCallFuncN callbacks or counting numberOfRunningActions.

Example.

In our app it is important to let the jumpAction to be finished prior to executing another jump. To prevent triggering another jump during an already running jump action the critical jump section of code is protected as follows:

#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
    }
}


You can always add a method to indicate when the method is finished, and then toggle some BOOL or something like that to indicate it is not running, and put a start method to toggle the BOOL to indicate it started:

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

Modified from here.

In the two @selector methods:

-(void) spriteMoveStarted:(id)sender {
    ccMoveByIsRunning = YES;
}

and:

-(void) spriteMoveFinished:(id)sender {
    ccMoveByIsRunning = NO;
}

where ccmoveByIsRunning is the BOOL I'm referring to.

EDIT: As xus has pointed out, you should actually not do this and instead use [self numberOfRunningActions] as others have pointed out.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜