2
votes

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

2
Shall try [CCSprite spriteWithFile:@"char1.png"]; ? - B.S.
it's works but I want to call the texture from a spritesheet - Hassy31
Is everything ok with sharedSpriteFrameCache? How do you add frames there? Also if it has problem with last path component try quickfix @"char1", maybe it has such name in the framechache - B.S.

2 Answers

2
votes

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

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

AddImage takes input imageName(NSString) not sprite(CCSprite).

[[CCTextureCache sharedTextureCache] addImage:TEX_IMAGE_NAME]; 

Here some useful code for you that solve your crash:

CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:@"char2.png"];
[crane setTexture:sprite.texture]; //Already allocated memory? if not thn try below
//crane = [CCSprite spriteWithTexture:sprite.texture];
//crane = [CCSprite spriteWithTexture:sprite.texture rect:rect];