Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the image of a CCSprite

I have created a number of CCSprites using spriteWithFile.

How do I change the image for the sprite during runtime?

I need to change a few sprites images quite regularly.

like image 274
user773578 Avatar asked Aug 01 '11 08:08

user773578


3 Answers

CCTexture *tex = [CCTexture textureWithFile:fileName];
self.texture = tex;
like image 87
Van Do Avatar answered Oct 22 '22 20:10

Van Do


If you are using SpriteSheets, this will work.

NSString* newSprite = [NSString stringWithString:@"SPRITE_NAME.png"];
CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[sprite setDisplayFrame:[cache spriteFrameByName:newSprite]];
like image 14
Clev3r Avatar answered Oct 22 '22 22:10

Clev3r


[yourSprite setTexture:[[CCSprite spriteWithFile:@"yourImage.png"]texture]];
like image 9
Zeeshan Avatar answered Oct 22 '22 22:10

Zeeshan