Cocos2d-x v4.0-Find subfolders in Resources folder

Hi i using spritecache->getSpriteFrameByName(“draw.png”) and its return null, this file is located in ‘Resources/character/idle/draw.png’, its seems dont find the subfolders. Any idea?
I search in forums and i must use
https://docs.cocos2d-x.org/api-ref/cplusplus/v4x/dc/d69/classcocos2d_1_1_file_utils.html

The question is, how i add a new folder in resources?, where the project detect it.
I’m using cocos2d-x in code-blocks.
Thanks

  1. have you already loaded “draw.png” previously? If not, you need to use it before it ends up in the spriteframe cache.

  2. use a spritesheet and load it when your game starts, then everything is in the cache at that time, continue doing what you are doing.

_searchPaths.push_back("res");
        
_fileUtils = cocos2d::FileUtils::getInstance();
_fileUtils->setSearchPaths(_searchPaths);
            
_spritecache = cocos2d::SpriteFrameCache::getInstance();
_spritecache->addSpriteFramesWithFile("sprites.plist");
  1. post a screenshot of your resources directory please.


i want detect the pngs inside character/idle/0001.png

Post this line of code in your project…my guess is that it says res too and in which case move everything to res/ or whatever directory this line says or add another path…

_searchPaths.push_back("res");
_searchPaths.push_back("Resources");

getSpriteFrameByName is only used if you’ve loaded a spritesheet that contains that specific frame in it, as @slackmoehrle shows in point 2 of the post above.

From your description and screenshot, it seems that you’re just trying to load an image from a file, so you need to use Sprite::create("filename.png"). For your specific case, it would be Sprite::create("character/idle/0001.png");.

If you want to add the character or character/idle to the search paths, then do this:

auto* fileUtils = FileUtils::getInstance();
fileUtils->addSearchPath("character");  // if you want to use Sprite::create("idle/0001.png"); etc.
fileUtils->addSearchPath("character/idle");  // only if you want to use Sprite::create("0001.png"); etc.

I don’t recommend you add those paths to the search list, because if you have multiple characters, and each has graphics that use the same file names, such as 0001.png, then it’ll always pick the first one it finds, which may not be the one you want.

1 Like

I want create a animation from 3 pngs in a folder and load it.

Vector<SpriteFrame*> HelloWorld::getAnimation(const char *format,int num){
    auto spritecache = SpriteFrameCache::getInstance();
    Vector<SpriteFrame*> animFrames(num) ;
    char str[100] = {0};
    for(int i = 0; i < num; ++i)
    {
        sprintf(str, format, i);
        auto frame = spritecache->getSpriteFrameByName(str);
        if(frame == nullptr){
            log("Null");
        }
        animFrames.pushBack(frame);
    }
    return animFrames;
}

Its still returning null.

Very true. I name everything different or prefix it with a unique letter before the number but your point is valid.

Something should appear on the console if it cannot be loaded. Does the console state anything?

I understand what you said,i will continue trying. Thanks for the answers.

Again, you cannot use getSpriteFrameByName, because the images in your resource path are not frames of a sprite sheet. That function only deals with frames that exist in a sprite sheet, and you’re not using sprite sheets. The name of that function is perhaps a little confusing, and it’s not doing what you think it is.

If you do want to use getSpriteFrameByName, then create a sprite sheet using TexturePacker app or similar (other free options out there).

I use to prefix names as well, but it became a maintenance issue, and realised it’s easier to use paths to distinguish between different resources, so regardless of how many images have the same name, the relative path to them is always unique.

2 Likes

@Madness188 f you want to do it the hard way, this is it:

auto* frameCache = SpriteFrameCache::getInstance();

auto* sprite = Sprite::create("character/idle/0001.png");
auto* spriteFrame = SpriteFrame::create(sprite->getTexture, sprite->getTextureRect());
frameCache->addSpriteFrame(spriteFrame, "character/idle/0001.png");

sprite = Sprite::create("character/idle/0002.png");
spriteFrame = SpriteFrame::create(sprite->getTexture, sprite->getTextureRect());
frameCache->addSpriteFrame(spriteFrame, "character/idle/0002.png");

sprite = Sprite::create("character/idle/0003.png");
spriteFrame = SpriteFrame::create(sprite->getTexture, sprite->getTextureRect());
frameCache->addSpriteFrame(spriteFrame, "character/idle/0003.png");

Too much code as you mentioned. The hard way.

I want access tutorials, but it not working.
https://cocos2d-x.org/wiki/How_To_Create_Sprites

Where did you find this link? We haven’t had a wiki in about 8 years.

Texture packer isn’t free.

There are free tools. I can’t say how well they work.

https://docs.cocos2d-x.org/cocos2d-x/v4/en/sprites/spritesheets.html