time delay for sprite, cocos2d

1.2k views Asked by At

i want to add a delay to a sprite so when i enter a new scene it doesnt play straight away, any advice would be greatful thanks

heres the code of the sprite if it helps

//SPRITES BIRD ONE -------------------------
    //------------------------------------------
    CCSpriteFrameCache *cache=[CCSpriteFrameCache sharedSpriteFrameCache];
    [cache addSpriteFramesWithFile:@"house-ipad.plist"];

    // frame array
    NSMutableArray *framesArray=[NSMutableArray array];
    for (int i=1; i<24; i++) {
        NSString *frameName=[NSString stringWithFormat:@"house-ipad%d.png", i];
        id frameObject=[cache spriteFrameByName:frameName];
        [framesArray addObject:frameObject];
    }

    // animation object
    id animObject=[CCAnimation animationWithFrames:framesArray delay:0.035];

    // animation action;
   id animAction=[CCAnimate actionWithAnimation:animObject restoreOriginalFrame:NO];
    //id animAction=[CCAnimate actionWithDuration:4 animation:animObject restoreOriginalFrame:NO];
    //animAction=[CCRepeatForever actionWithAction:animAction];

    // sprite (width,height)
    CCSprite *bird=[CCSprite spriteWithSpriteFrameName:@"house-ipad1.png"];

    bird.position=ccp(550,470);


    // batchNode
    CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"house-ipad.png"];
    [self addChild:batchNode];
    [batchNode addChild:bird];

    [bird runAction:animAction];

    //-----------------------------------------
1

There are 1 answers

1
N C On

create a new method for adding new sprite and call this after delay:

[self performSelector:@selector(afterDelay:) withObject:animAction afterDelay:0.5];

-(void)afterDelay:(id)animAction
{
// sprite (width,height)
 CCSprite *bird=[CCSprite spriteWithSpriteFrameName:@"house-ipad1.png"];

bird.position=ccp(550,470);


// batchNode
CCSpriteBatchNode *batchNode=[CCSpriteBatchNode batchNodeWithFile:@"house-ipad.png"];
[self addChild:batchNode];
[batchNode addChild:bird];

[bird runAction:animAction];
}