0

I have a cocos2d iOS app with Box2D (and Kobold2D); i have an array of 18 CCSprites in a layer. They are now created using spriteWithSpriteFrameName and a textureAtlas (thank you texturePacker). When i want to update the 18 sprites, i think i can either a) change the image (but i am not how to do that -- i saw a reference to setDisplayFrame, but i need to get the image from the batch node / texture atlas using spriteWithSPriteFrameName) or b) destroy the sprite i previously created and added to the layer with addChild and create a new one it it's place (18 sprites, 16 times in one "game"). In terms of resource usage and performance, which method is preferred? Seems like a), but again, not sure how to do that.

thanks

harry
  • 340
  • 4
  • 13

2 Answers2

4

You could add the following code as extension to CCSprite:

-(void) setDisplayFrameNamed:(NSString*)name
{
    [self setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:name]];
}

If you are using box2d you could also use GBox2D which is described in detail in the MonkeyJump Tutorial

Andreas Löw
  • 1,002
  • 8
  • 15
0

The ideal solution is not to switch textures of a sprite at all if you can avoid it. Second best is changing texture via Spriteframe (note that this rules out the use of a CCSpriteBatchNode).

Creating new sprites is typically the operation which has the highest negative performance impact.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217