Search code examples
ioscocos2d-iphonetouches

Prevent Cocos2D touches during animation?


In this animation here:

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

I am trying to simply fade out/in labels. The thing is though, I want to make sure that this method won't get called while the labels are animating.

If you look in the code, I attempt to do that by calling this method:

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

But it does not seem to work. If I touch the screen while the labels are animating, it messes up the animation midway through and doesn't do what I want.

Any ideas?

Thanks!


Solution

  • I ended up doing this in case anyone has the same issue:

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