循环数组:每个元素的CCAction,但操作完成后只有一个回调

时间:2014-08-17 19:03:02

标签: ios objective-c cocos2d-iphone

我使用UIPanGestureRecognizer来检测平移/滑动。滑动完成并且触摸结束时,我在NSSet中的12个CCNodeColors上运行CCAction。

当CCActions正在运行时,我禁用UIPanGestureRecognizer,以便用户在上一个动画仍在运行时无法滑动。 CCActions完成后,我再次启用UIPanGestureRecognizer。

问题: *callback操作运行12次。因此,在完成其他11个操作之前启用UIPanGestureRecognizer。在最后一次操作*callback完成后仅运行*move的最佳方式是什么?

(或者它是一个问题吗?)

以下是简化代码:

if (sender.state == UIGestureRecognizerStateEnded ) {

    sender.enabled = NO;

    for (CCNodeColor *node in nodeSet) {

        CCActionMoveTo *move = [CCActionMoveTo actionWithDuration:1.0 position:newPosition];

        CCActionCallBlock *callback = [CCActionCallBlock actionWithBlock:^{
            sender.enabled = YES;
        }];

        CCActionSequence *seq = [CCActionSequence actionWithArray:@[move, callback]];
        [node runAction:seq];
    }
}

1 个答案:

答案 0 :(得分:0)

这是一个简化的'可能的答案

if (sender.state == UIGestureRecognizerStateEnded ) {

    sender.enabled = NO;

    for (CCNodeColor *node in nodeSet) {
        CCActionMoveTo *move = [CCActionMoveTo actionWithDuration:1.0 position:newPosition];
       [node runAction:move];
    }

    CCActionDelay *wait = [CCActionDelay actionWithDuration:1.05f]; 
    CCActionCallBlock *callback = [CCActionCallBlock actionWithBlock:^{
        sender.enabled = YES;
    }];
    CCActionSequence *seq = [CCActionSequence actionWithArray:@[wait, callback]];
    [self runAction:seq];

}