Steve
Steve

Reputation: 6362

Cocos2d fade in/out action to repeat forever

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!

Upvotes: 7

Views: 23330

Answers (2)

Nathaniel Rogers
Nathaniel Rogers

Reputation: 259

I had the same problem and it took me a loooong time to figure out why.

when you create CCSequences I found that you have to copy the CCAction.

In your case.

CCAction *fadeIn = [CCFadeTo actionWithDuration:0.5 opacity:0.5];
CCAction *fadeOut = [CCFadeTo actionWithDuration:0.5 opacity:1.0];

CCSequence *pulseSequence = [CCSequence actions:
                             [fadeIn copy], 
                             [fadeOut copy], 
                             nil];

Hope I helped.

Upvotes: -1

Steinbitglis
Steinbitglis

Reputation: 2614

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

Upvotes: 23

Related Questions