7

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
  • 6,332
  • 11
  • 41
  • 53

2 Answers2

23

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];
}
Steinbitglis
  • 2,482
  • 2
  • 27
  • 40
  • 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 May 29 '11 at 21:24
  • although if I change `CCAction` to `CCFiniteTimeAction` the error goes away and it works... why would that be? – Steve May 29 '11 at 21:26
  • 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. – Steinbitglis May 29 '11 at 21:31
  • 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? – Steve May 30 '11 at 01:00
  • Yes, it's the equivalent of 0xff – Steinbitglis May 30 '11 at 01:44
  • 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 May 23 '14 at 14:46
  • How do you change the duration of the above fade to dynamically? – Ankish Jain May 28 '14 at 06:44
-1

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.

Nathaniel Rogers
  • 249
  • 4
  • 10