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.
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.
CCTexture *tex = [CCTexture textureWithFile:fileName];
self.texture = tex;
If you are using SpriteSheets, this will work.
NSString* newSprite = [NSString stringWithString:@"SPRITE_NAME.png"];
CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[sprite setDisplayFrame:[cache spriteFrameByName:newSprite]];
[yourSprite setTexture:[[CCSprite spriteWithFile:@"yourImage.png"]texture]];
In COCOS2D-X you can do this by following way
CCTexture2D *tex = CCTextureCache::sharedTextureCache()->addImage("xyz.png");
sprit_name->setTexture(tex);
iF YOU WANT TO CHANGE THE SPRITE SIZE THEN ALSO WRITE THIS LINE
sprit_name->setTextureRect(CCRectMake(0,0, tex->getContentSize().width, tex->getContentSize().height));
i code cocos2D-x in c++ : when you use spriteSheet instead of using single file, you shoud use this method. suppose you add your spriteSheet in main layer.
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite_sheet.plist");
_gameBatchNode = CCSpriteBatchNode::create("sprite_sheet.png", 200);
this->addChild(_gameBatchNode, kMiddleground);
kMiddleground is just a defined integer.
then i want to change picture of a sprite that already has a sprite with name "cloud.png" i use this code to do that:
cloud->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("blackCloud.png") );
For those who are using Cocos2d-iPhone v 3.0.0 or later
demonSprite.texture = [[CCSprite spriteWithImageNamed:@"steelDemonNormal.png"] texture];
if You are using SpriteSheets then this will work
CCSpriteBatchNode *batch;
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFrameWithFile:@"file.plist"];
batch = [[[CCSpriteBatchNode alloc] initWithFile:@"file.png"] capacity:50] autorelease];
[self addChild:batch];
Try using atlassed textures for you game because you are using same sprite that too with many instances
Use
CCSpriteFrameCache, CCSpriteBatchNode
So you can optimize the texture cache with even more different textures atlassed in a single texture. And all are batched in a single node. This reduces the number of draw calls too and increases frame rate.
Try using
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Characters.plist"];
CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"Characters.png" capacity:10];
[self addChild:batch];
CCSprite* player1 = [CCSprite spriteWithSpriteFrameName:@"Luce.png"];
player1.position = ccp(winSize.width * 0.2, winSize.height * 0.8);
[batch addChild:player1];
and use
CCSprite* player = nil;
CCARRAY_FOREACH(batch.children, player1) {
// some computational code and condition
if (sonecondition) {
[player1 setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"spriteframename.png"]];
}
}
in update or in other parts of code
Use Zwoptex
TexturePacker to create 2D texture atlasses
Hope this helps
Good luck
if you want to change the images continuously then write following code in your init method
CCTexture2D *tex1 = [[CCTextureCache sharedTextureCache] addImage:@"image1.png"];
CCTexture2D *tex2 = [[CCTextureCache sharedTextureCache] addImage:@"image2.png"];
CCTexture2D *tex3 = [[CCTextureCache sharedTextureCache] addImage:@"image3.png"];
CCSprite *sprite = [CCSprite spriteWithTexture:tex1];
//Make above variables as class level to access in whole class
[self addChild:sprite];
//position the sprite according to your need
write this line where you want to change the image
[sprite setTexture:tex3];
you need to make one cctexture2d object in such a way
CCTexture2D* newtexture = [[CCTextureCache sharedTextureCache] addImage:@"new-texture"]; [mainSprite setTexture: newtexture];
this is a whole thing needed to be done if you want change sprite image texture run time.
You just need to change the texture of your CCSprite at runtime like this.
[aSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"NewImage.png"]];