xcodecocos2d-iphonespriteccspritesprite-sheet

Change a sprite texture, with image from a sprite-sheet


This is how I create a sprite with texture form a sprite-sheet. But how do I change the sprites texture later on?

I have tried using setTexture but I can't get i working.

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"gameSpriteSheet.plist"];        
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"gameSpriteSheet.png"];
[self addChild:spriteSheet];

// Add hangman graphic sprite to Scene
playerSprite = [CCSprite spriteWithSpriteFrameName:@"playerX"];
playerSprite.position = ccp(580, 400);
[self addChild:playerSprite];

Solution

  • You can't.

    A CCSpriteBatchNode uses a single texture. All of the sprites added to the CCSpriteBatchNode must use the same texture - that of the CCSpriteBatchNode.

    I'm sure there's a different solution to the problem you're facing, give it some thought from a different angle. For example you could achieve the same effect by simply creating a new sprite.

    If you're looking to change just the CCSpriteFrame of the sprite, use setDisplayedFrame.