cocos2d-iphoneccsprite

How to change the image of a CCSprite in cocos2d v3.x


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.


Solution

  • 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.