Reputation: 275
I've tried
[[CCTextureCache sharedTextureCache] addImage: @"still.png"];
But I always end up with a distorted image for some reason. It's most likely because my images are not the same resolution, but for this app, they can't have the same res. How do I change the sprite's image without going through the complicated process of making a spritesheet or an animation or any of that.
Upvotes: 9
Views: 17632
Reputation: 12747
In Cocos2d-x v3, you can use my_sprite->setTexture(sprite_path);
Upvotes: 0
Reputation: 17523
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. :)
Upvotes: 1
Reputation: 233
I use cocos2d 3.0 and this code works for me:
[_mySprite setTexture:[CCTexture textureWithFile:@"myFile.png"]];
Upvotes: 0
Reputation: 386
This simple one line can do your task.
[sprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"slot.png"]];
Upvotes: 0
Reputation: 1296
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"]];
Upvotes: 16
Reputation: 1377
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!
Upvotes: 6
Reputation: 6397
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"] ];
Upvotes: 10