tallen11
tallen11

Reputation: 1377

Most efficient way to create CCSprites and particles in Cocos2D

Right now, in my game, I am spawning a sprite every second or so at the top of the screen (using a sceduler) using this code:

The init method:

[self schedule:@selector(addMeteor:) interval:1];

The scheduler method:

- (void)addMeteor:(ccTime)dt
{
    CCTexture2D *meteor = [[CCTextureCache sharedTextureCache] addImage:@"Frame3.png"];
    target = [CCSprite spriteWithTexture:meteor rect:CGRectMake(0, 0, 53, 56)];
    //Rest of positioning code was here
}

Doing it this way causes a stutter in the frame rate every second or so (Whenever another sprite is spawned). Is there a way to eliminate that?

Thanks in advance!

Tate

Upvotes: 0

Views: 523

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

I'm guessing the stutter is more likely coming from other parts of the code. Do you explicitly call removeChild on meteors? That might cause a hiccup, especially with many meteors.

My advice: create N meteor sprites up front. When you need one, make it visible and change its position. When you're done with it, set it to visible = NO to make it disappear.

Upvotes: 2

Related Questions