c++cocos2d-xtexturepacker

Use multiple sprite sheets at once in Cocos2d-x


I'm making a platformer videogame in Cocos2d-x C++.

What I want to do is really easy but everything I have found on the internet either doesn't work or it's for another programming language like Objective-C.

I basically have multiple Spritesheets I'd like to use on my game. I can't merge them into 1 big spritesheet because they are really big.

I just want to know how to include multiple spritesheets so I can use them.

For using 1 spritesheet I use the following code:

// load and cache the texture and sprite frames
auto cacher = SpriteFrameCache::getInstance();
cacher->addSpriteFramesWithFile("GJ_GameSheet02-hd.plist");

I've read that I can use SpriteBatch Node for that, but I haven't found anything related to how to use that on C++.

Any help would be appreciated. Thanks


Solution

  • I used the advanced feature of Texture Packer: generate .h and .cpp files. But all you need is:

    cache->addSpriteFramesWithFile("Graphics-0.plist", "Graphics-0.png");
    
    cache->addSpriteFramesWithFile("Graphics-1.plist", "Graphics-1.png");
    

    You only need to call addSpriteFramesWithFile multiple times with all your .plist files and .png

    if you have a lot you can create a for loop:

    void addSpriteFramesToCache(int i)
    {
    SpriteFrameCache *cache = SpriteFrameCache::getInstance();
    
        cache->addSpriteFramesWithFile("Graphics-" + std::to_string(i) + ".plist",
        "Graphics-" + std::to_string(i) + ".png");
    }
    
    for (int i = 0; i < numberOfFramesToLoad; i++) //
    {
        addSpriteFramesToCache(i);
    }