1

The delayperunit property in my code below keeps the sprite on screen for 2.5 seconds before it changes. What I actually want to do is display the sprite for 0.5 seconds so that there is 2.0 second break (no animation displayed) before the next one appears for another 0.5 seconds and so on. How can I achieve this?

  // Create the intro image
CGSize screenSize = [CCDirector sharedDirector].winSize;
CCSprite *introImage = [CCSprite spriteWithFile:@"intro1.png"];
[introImage setPosition:ccp(screenSize.width/2, screenSize.height/2)];
[self addChild:introImage];

// Create the intro animation, and load it from intro1 to intro7.png
CCAnimation *introAnimation = [CCAnimation animation];
[introAnimation delayPerUnit:2.5f];
for (int frameNumber=0; frameNumber < 8; frameNumber++) {
    CCLOG(@"Adding image intro%d.png to the introAnimation.",frameNumber);
    [introAnimation addSpriteFrameWithFilename:
     [NSString stringWithFormat:@"intro%d.png",frameNumber]];
}
oopology
  • 1,072
  • 1
  • 10
  • 19

2 Answers2

1

I dont think you can do that with a CCAnimation. You could either embed that in a class (if this will be a recurring theme), or do this in your module that displays these frames:

in .h, some ivars;

  NSUInteger _currentFrame;
  NSUInteger _maxFrames;

in .m, where you are ready to begin:

 _currentFrame=1;
 _maxFrames = 8;
 [self scheduleOnce:@selector(flashFrame) delay:0.];

and

-(void) flashFrame: {

    NSString *spriteName = [NSString stringWithFormat:@"intro%d.png",_currentFrame];
    CCSprite *sprite = [CCSprite spriteWithFile:spriteName];
    CGSize screenSize = [CCDirector sharedDirector].winSize;
    sprite.position=ccp(screenSize.width/2, screenSize.height/2);
    id wait  = [CCDelayTime actionWithDuration:.5];
    id clean = [CCCallBlock actionWithBlock:^{
       [sprite removeFromParentAndCleanup:YES];

       }];
    id seq   = [CCSequence actions:wait,clean,nil];
    [self addChild sprite];
    [sprite runAction:seq];
    if(_currentFrame < maxFrame) {
       _currentFrame++;
       [self scheduleOnce:@selector(flashFrame) delay:2.0];
    } else {
       // do whatever you wanted to do after this sequence.
    }

}

standard disclaimer : not tested, coded from memory, mileage will vary :)

YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48
0

Consider having an action composed by a CCSequence of CCAnimation (the one with 0.2 delay per unit) and add a CCDelayTime of 0.5 secs in between the animations. Then Repeat the action forever. Also, use CCAnimationCache to store your animations if you are using Cocos2d 2.x onwards.

mm24
  • 9,280
  • 12
  • 75
  • 170