I\'ve tried
[[CCTextureCache sharedTextureCache] addImage: @\"still.png\"];
But I always end up with a distorted image for some reason. It\'s m
urSprite = [CCSprite spriteWithFile:@"one.png"]; urSprite.position = ccp(240,160); [self urSprite z:5 tag:1]; // Changing the image of the same sprite [urSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"two.png"]];
In Cocos2d-x v3, you can use my_sprite->setTexture(sprite_path);
This is the most straight forward way to change the image of a sprite(if you have it loaded trough a spritesheet) this definitely works (I use it all the time in my game). mySprite is the name of the sprite instance:
[mySprite setDisplayFrame:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName: @"sprite1.png"] ];
You would just call the sprite.texture function.
Example
In your init method:
CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:@"still.png"];
CCTexture2D *tex2 = [[CCTextureCache sharedTextureCache] addImage:@"anotherImage.png"];
CCSprite *sprite = [CCSprite spriteWithTexture:tex1];
//position the sprite
[self addChild:sprite];
Then to change the image of the sprite to tex2:
sprite.texture = tex2;
Very simple!
Hope this helped!
In cocos2d v3, I was able to accomplish this with...
[mySprite setSpriteFrame:[CCSpriteFrame frameWithImageNamed:@"two.png"]]
...but I have no idea if this has side effects that will hurt me in the long run. :)
I use cocos2d 3.0 and this code works for me:
[_mySprite setTexture:[CCTexture textureWithFile:@"myFile.png"]];