动画期间防止Cocos2D触摸?

时间:2011-11-19 00:20:54

标签: ios cocos2d-iphone touches

在这个动画中:

    - (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    self.isTouchEnabled = NO;
    if (scoreLabel.opacity == 225) {
        NSLog(@"fadeOut");
        CCSequence *fadeOut = [CCSequence actions:[CCFadeOut actionWithDuration:0.5], [CCCallFuncN actionWithTarget:self 
        selector:@selector(enableTouches)], nil];
        [scoreLabel runAction:fadeOut];
        [livesLabel runAction:[[fadeOut copy] autorelease]];
    }
    else {
        NSLog(@"fadeIn");
        CCSequence *fadeIn = [CCSequence actions:[CCFadeIn actionWithDuration:0.5], [CCCallFuncN actionWithTarget:self 
        selector:@selector(enableTouches)], nil];
        [scoreLabel runAction:fadeIn];
        [livesLabel runAction:[[fadeIn copy] autorelease]];
         }
}

我试图简单地淡出标签。但事情是,我希望确保在标签动画时不会调用此方法。

如果您查看代码,我会尝试通过调用此方法来执行此操作:

    - (void)enableTouches {
    NSLog(@"ET");
        self.isTouchEnabled = YES;
}

但它似乎不起作用。如果我在标签动画时触摸屏幕,它会在中途混淆动画并且不会做我想要的。

有什么想法吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

如果有人遇到同样的问题,我最终会这样做:

- (void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
    self.isTouchEnabled = NO;

    if(label1.opacity == 0 )
    {
        CCFadeIn* fadeIn = [CCFadeIn actionWithDuration:0.5];
        CCCallBlock* fadeCompleted = [CCCallBlock actionWithBlock:^{ self.isTouchEnabled = YES; }];     

        [label1 runAction:[CCSequence actions:fadeIn, fadeCompleted, nil]];
        [label2 runAction:[[fadeIn copy] autorelease]];
    }
    else
    {
        CCFadeOut* fadeOut = [CCFadeOut actionWithDuration:0.5];
        CCCallBlock* fadeCompleted = [CCCallBlock actionWithBlock:^{ self.isTouchEnabled = YES; }];     

        [label1 runAction:[CCSequence actions:fadeOut, fadeCompleted, nil]];
        [label2 runAction:[[fadeOut copy] autorelease]];
    }
}

答案 1 :(得分:1)

如果您不知道,可以使用与为节点分配标签的方式相同的方式为操作分配标签。然后,您可以在运行该操作的对象上调用getActionByTag:获取nil或指向该操作的指针。通过检查返回值是否为nil或动作,您将知道您正在寻找的某个动作/动画是否正在播放。这样你就可以在没有额外变量的情况下做到这一点。