Search code examples
ioscocos2d-iphone

CCAction block "tweening"


Is there a CCAction similar to the SKAction in SpriteKit which get's called multiple times for the duration of the action (so you can create custom effects based on the time that has passed?)

I have found the CCActionCallBlock, but this is only called once and has no parameter for the "time passed."

E.g something like this (SpriteKit) in Cocos2D.

SKAction *customACtion = [SKAction customActionWithDuration:ANIMATION_TIME actionBlock:^(SKNode *node, CGFloat elapsedTime) {
    // Do something here
}];

The effect I am trying to achieve is this. Which in SpriteKit I can do like so:

#define ANIMATION_TIME 2

SKAction *customACtion = [SKAction customActionWithDuration:ANIMATION_TIME actionBlock:^(SKNode *node, CGFloat elapsedTime) {
    // Determine the percentage of the elapsed time
    float percentage = (elapsedTime / ANIMATION_TIME) * 100;
    // Put the result in a label
    self.label.text = [NSString stringWithFormat: @"%d", percentage];
}];

Solution

  • In cocos2d you can create multiple CCActions and put them in a container(CCSequence) in order to run one after another.

    Here is an example of Composite actions..

    id action1 = [CCMoveTo actionWithDuration:2 position:ccp(100,100)];
    id action2 = [CCMoveBy actionWithDuration:2  position: ccp(80,80)];
    id action3 = [CCMoveBy actionWithDuration:2  position: ccp(0,80)];
    [sprite runAction: [CCSequence actions:action1, action2, action3, nil]];
    

    The action1 will be executed first. When action1 finishes, then action2 will be executed. And when action2 finishes, only then action3 will be executed .

    For More detail refer to this OFFICAL LINK

    This can be dnoe by this..

     [self schedule: @selector(updateLabel:) interval:0.1] // Set interval,repeat whatever you want
    
     -(void) updateLabel: (ccTime) dt
      {
         if(dt>2000){
           [self unschedule:@selector(updateLabel)];
        }
         float percentage =percentage+ (dt/ (ANIMATION_TIME * 10));
          // Put the result in a label
         self.label.text = [NSString stringWithFormat: @"%d", percentage];
    
      }