禁用CCbutton直到CCActionSequence完成

时间:2014-08-13 11:22:16

标签: objective-c cocos2d-iphone

我有一些类似下面的代码:

GameScene.m

CCButton *yesButton = [CCButton buttonWithTitle:"@YES"];
[yesButton setTarget: self selector@selector(yesButtonTapped:)];
[self addChild: yesButton];

-(void)yesButtonTapped: (id)sender
{
   if(match == 1) {
      [_hud updateBallNumber: ball];
   }
}

HUDLayer.m

-(void)updateBallNumber: (Ball*)ball 
{
    int number = [ball getNumber];
    id fadeIn = [CCActionFadeIn actionWithDuration:1];
    id fadeOut = [CCActionFadeOut actionWithDuration:1];
    id delay = [CCActionDelay actionWithDuration: randomDelay];
    id change = [CCActionCallBlock actionWithBlock:^{
        _ballLabel.string = [NSString stringWithFormat @"%i",number}];

    id sequence = [CCActionSequence actions: fadeOut,delay,change,fadeIn,nil];

    [ballLabel runAction:sequence];
}

按照我的预期,当按下yesButton时,球标签中显示的当前数字淡出等待一段随机时间,然后淡入新数字。

我的问题是如何停止再次按下yesButton或忽略按钮按钮,直到序列完成并显示新号码。

我尝试过使用yesButton.enable,将其设置在-(void)yesButton的开头到NO,然后在YES的末尾设置,但这并不等待序列完成。

1 个答案:

答案 0 :(得分:0)

你可以用积木做到这一点。将updateBallNumber更改为

某处:

 typedef void(^VoidBlock)();

在HUDLayer.m

-(void)updateBallNumber: (Ball*)ball withCompletion:(VoidBlock) completionBlock
{
    int number = [ball getNumber];
    VoidBlock atEnd = [completionBlock copy];  // MRC, not certain how this pans out in ARC

    id fadeIn = [CCActionFadeIn actionWithDuration:1];
    id fadeOut = [CCActionFadeOut actionWithDuration:1];
    id delay = [CCActionDelay actionWithDuration: randomDelay];
    id change = [CCActionCallBlock actionWithBlock:^{
       _ballLabel.string = [NSString stringWithFormat @"%i",number}];
    id complete = [CCActionCallBlock actionWithBlock:^{
        atEnd();
        [atEnd release];        // MRC
    }];
    id sequence = [CCActionSequence actions: fadeOut,delay,change,fadeIn,complete,nil];

    [ballLabel runAction:sequence];
}

在GameScene中

yesButton.enabled = NO; 
[_hud updateBallNumber:ball withCompletion:^{
    yesButton.enabled = YES; 
}];