objective-ccocos2d-iphonecctexturecache

cocos2d change sprite texture


I want to change a sprite texture with another texture from a sprite sheet

//normal sprite
character = [CCSprite spriteWithSpriteFrameName:@"char1.png"];

//change texture
[character setTexture:[[CCTextureCache sharedTextureCache] addImage:[CCSprite spriteWithSpriteFrameName:@"char2.png"]]];

but this gives me an error.I have tried without spriteWithSpriteFrameName and it gives me a blank texture.

//error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite lastPathComponent]: unrecognized selector sent to instance 0xab32580'

please help me to solve this problem


Solution

  • Try setting the displayFrame on the CCSprite, e.g.,

    [character setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                            spriteFrameByName:@"char2.png"];