33

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.

user773578
  • 1,161
  • 2
  • 13
  • 24

11 Answers11

58
CCTexture *tex = [CCTexture textureWithFile:fileName];
self.texture = tex;
buildsucceeded
  • 4,203
  • 4
  • 34
  • 72
Van Do
  • 596
  • 6
  • 2
  • 5
    what to do if texture is different in size from previous one ? – Faheem Rajput Jan 16 '13 at 13:04
  • 10
    @FaheemRajput.. If the sprites are of different size then you will need to do this after calling setTexture function --- [spriteWant2Change setTextureRect:CGRectMake(0, 0, tex.contentSize.width, tex.contentSize.height)];--- – Arslan Apr 16 '13 at 05:32
  • @Arslan your comment is quite helpful. Thanks. :) – Ravi Bhatt Nov 19 '13 at 06:32
  • 2
    Anyone knows how to do this in cocos2d 3.x ? Because xcode doesn't find CCTextureCache anymore. – lsmpascal Mar 01 '14 at 19:51
  • is one cctexture on another Cctexture overlap it or remove it from cache ?? what exactly happens we setTexture ? – shaqir saiyed Jun 12 '14 at 06:45
  • 1
    @PaxMaximinus you can do so by aSprite.texture = [[CCSprite spriteWithImageNamed:@"steelDemonNormal.png"] texture]; – Sauvik Dolui Oct 07 '14 at 10:20
14

If you are using SpriteSheets, this will work.

NSString* newSprite = [NSString stringWithString:@"SPRITE_NAME.png"];
CCSpriteFrameCache* cache = [CCSpriteFrameCache sharedSpriteFrameCache];
[sprite setDisplayFrame:[cache spriteFrameByName:newSprite]];
Clev3r
  • 1,568
  • 1
  • 15
  • 28
9
[yourSprite setTexture:[[CCSprite spriteWithFile:@"yourImage.png"]texture]];
Steve Robbins
  • 13,672
  • 12
  • 76
  • 124
iC7Zi
  • 1,528
  • 2
  • 15
  • 21
  • 1
    Cocos2d v3 would be: [yourSprite setTexture:[[CCSprite spriteWithImageNamed:filename]texture]]; – mevdev Mar 22 '14 at 20:18
4

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));
Singhak
  • 8,508
  • 2
  • 31
  • 34
1

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") );
mostafa88
  • 532
  • 1
  • 8
  • 20
1

For those who are using Cocos2d-iPhone v 3.0.0 or later

demonSprite.texture = [[CCSprite spriteWithImageNamed:@"steelDemonNormal.png"] texture];
Sauvik Dolui
  • 5,520
  • 3
  • 34
  • 44
0

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];
paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
Gaurav
  • 721
  • 5
  • 14
0

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

Kumar C
  • 525
  • 6
  • 21
0

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];

Ali Raza
  • 613
  • 5
  • 17
0

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.

Muhammad Shauket
  • 2,643
  • 19
  • 40
0

You just need to change the texture of your CCSprite at runtime like this.

[aSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:@"NewImage.png"]];
zoom8amit
  • 332
  • 2
  • 9