0

I used CCSpeed action in cocos2d iPhone game. But crashes all the time.

CCAnimation* animation = nil;
animation = [[CCAnimationCache sharedAnimationCache]  animationByName:ATTACK_ANIM];

if(animation)
{
    CCAnimate *animAction  = [CCAnimate actionWithAnimation:animation];

    id speed = [CCSpeed actionWithAction:animAction speed:0.13f];
    id calBlock = [CCCallBlock actionWithBlock:^{
                                                    //[self updateState:kTileState_Idle];
                                                }];
    CCSequence *sequence = [CCSequence actions:speed, calBlock, nil];        
    [self runAction:sequence];
}

But below code works fine..but not able to change animation speed. What's wrong with above code?

    CCAnimation* animation = nil;
    animation = [[CCAnimationCache sharedAnimationCache]  animationByName:quakeAnim];

    if(animation)
    {
        CCAnimate *animAction  = [CCAnimate actionWithAnimation:animation];
        id calBlock = [CCCallBlock actionWithBlock:^{
                                                        //[self updateState:kTileState_Idle];
                                                    }];
        CCSequence *sequence = [CCSequence actions:animAction, calBlock, nil];        
        [self runAction:sequence];
    }

Here is one anther thread. But no code solution provided.

Community
  • 1
  • 1
Guru
  • 21,652
  • 10
  • 63
  • 102

2 Answers2

1

Could you not set animation.delayPerUnit to some suitable value to alter its speed of execution ? I generally compute that number prior to each time i use it ... thus i dont need to persist the initial 'delayPerUnit' i set prior to placing the animation in the cache.

float totalAnimationTime = kSomeDesiredValue; // game logic dictates this, as well as
                                              // rendering requirements (too slow will be perceived)
animation.delayPerUnit = totalAnimationTime/animation.totalDelayUnits;

// totalDelayUnits is a property of the animation, usually equal to the number
// of frames.
YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
  • we can't change delayPerUnit once it is loaded. I need to change animation speed dynamically as game progresses. – Guru Mar 06 '13 at 16:19
  • 1
    the property is not readonly, so you can change it during game play. If you are concerned about concurrency (multiple instances), make a copy of the cached animation and change delayPerUnit on the copy, then run the copy. – YvesLeBorg Mar 06 '13 at 16:23
1

You're doing it wrong. ;)

CCSpeed can not be used in a sequence. You still need to add the animAction to the sequence, but keep a reference to the CCSpeed action (ivar). Then, whenever you want to change animation speed, change the speed property of the CCSpeed instance.

Using CCSped requires that you manage memory of the sequence and CCSpeed appropriately.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • not getting..give me one code example please(Usage of CCSpeed). Searched in net but no use. – Guru Mar 07 '13 at 04:50