2

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

Guru
  • 21,652
  • 10
  • 63
  • 102
Hassy31
  • 2,793
  • 3
  • 21
  • 37
  • Shall try [CCSprite spriteWithFile:@"char1.png"]; ? – B.S. Mar 21 '13 at 11:31
  • it's works but I want to call the texture from a spritesheet – Hassy31 Mar 21 '13 at 11:35
  • 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. Mar 21 '13 at 11:37

2 Answers2

2

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];
Guru
  • 21,652
  • 10
  • 63
  • 102
2

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

[character setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] 
                        spriteFrameByName:@"char2.png"];
gberginc
  • 447
  • 2
  • 4