3

In cocos2d 2.x we change the image of a CCSprite using CCTexture. But in cocos2d 3.x CCTextureCache seems to be deprecated as Xcode warns me : "undeclared identifier 'CCTextureCache'". Or may be am I miss something as I'm new to cocos.

So how can we change the image of a CCSprite in v3 ??

Thank you.

James Webster
  • 31,873
  • 11
  • 70
  • 114
lsmpascal
  • 762
  • 8
  • 24

2 Answers2

8

I think I know how to do.

  1. We have to use a spriteSheet built with TexturePacker [note : may be it's wrong to speak about external resources like it on SO] for example (let's say we have 2 images : monster_01.png and monster_02.png).
  2. We add the .plist and the .png into xCode
  3. We put the spritesheet in cache
  4. and then we can create a CCSprite with a frame using a item of the spritesheet.
  5. This image can be changed.

Some code :

3) We put in cache

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"monsterSpriteSheet.plist"];

4) We create the sprite

CCSprite * mySprite = [CCSprite initWithSpriteFrame: [CCSpriteFrame frameWithImageNamed: @"monster_01.png"]];

5) To change image :

 [mySprite setSpriteFrame:[CCSpriteFrame frameWithImageNamed: @"monster_02.png"]];

This works perfectly with cocos2d v3.

I spent 6 hours to have this process. Sometimes I feel stupid.

Lux
  • 17,835
  • 5
  • 43
  • 73
lsmpascal
  • 762
  • 8
  • 24
  • Awesome. Don't feel stupid. 90%+ of google searches for this question will gave me answers for cocos2d v2 or early, which are flat out wrong. I was thrill to find your post. One correction though. I think it should be spriteWithSpriteFrame, not initWithSpriteFrame. – honkskillet Aug 03 '14 at 03:01
0

You can do it by using this

CCSpriteFrameCache and after that you can change you sprite by using function setSpriteFrame of ccsprite.

Muhammad Shauket
  • 2,643
  • 19
  • 40