Cocos2d fade in/out action to repeat forever

Steve picture Steve · May 29, 2011 · Viewed 23.2k times · Source

I'm trying to make a method for my CCSprite based Player class to start the player instance fading in and out until stopped by calling stopAllActions.

In my Player class I have:

- (void)pulse
{
    [self setOpacity:1.0];
    CCAction *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:0.5];
    CCAction *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:1.0];

    CCSequence *pulseSequence = [CCSequence actions:
                                 fadeIn, // I get a warning about incompatible pointer types...
                                 fadeOut, 
                                 nil];
    [self runAction:pulseSequence];
}

This doesn't work and doesn't address the repeat forever part. I know I should probably use CCRepeatForever but I'm not seeing how to implement it correctly.

Thanks!

Answer

Steinbitglis picture Steinbitglis · May 29, 2011

I have not run this, but I think others have succeeded with something like:

- (void)pulse
{
    [self setOpacity:1.0];
    CCFadeTo *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:127];
    CCFadeTo *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:255];

    CCSequence *pulseSequence = [CCSequence actionOne:fadeIn two:fadeOut];
    CCRepeatForever *repeat = [CCRepeatForever actionWithAction:pulseSequence];
    [self runAction:repeat];
}