Cocos2d fade in/out action to repeat forever

23,277

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];
}
Share:
23,277
Steve
Author by

Steve

Full time: husband, father and engineer. Part time: gardener, handyman, and now programmer. My iOS apps - PoolSnap (Pool chemistry assistant), Daybreak (Sunrise/set calculator)

Updated on February 26, 2020

Comments

  • Steve
    Steve about 4 years

    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!

  • Steve
    Steve almost 13 years
    I'm still gettting the incompatible pointer errors for both fade actions. ~/Player.m: warning: Semantic Issue: Incompatible pointer types sending 'CCAction *' to parameter of type 'CCFiniteTimeAction *'
  • Steve
    Steve almost 13 years
    although if I change CCAction to CCFiniteTimeAction the error goes away and it works... why would that be?
  • Steinbitglis
    Steinbitglis almost 13 years
    I understand now, you typecasted to CCAction when you didn't have to. CCFadeTo inherits CCFiniteTimeAction. That is all CCSequence needs to know. I edited my answer accordingly.
  • Steve
    Steve almost 13 years
    Thanks - works now too. One more thing... the opacity property should be set to 255 in the first line if I want it fully visible, right?
  • Steinbitglis
    Steinbitglis almost 13 years
    Yes, it's the equivalent of 0xff
  • Ankish Jain
    Ankish Jain almost 10 years
    if for above pulse animation i want to increase or decrease animation speed dynamically on some action , how do to ?? Been searchin a lot , dint get an answer.got a deadline soon ! thnx
  • Ankish Jain
    Ankish Jain almost 10 years
    How do you change the duration of the above fade to dynamically?