How does FileUtils::fullPathForFilename work¶
This document is based on cocos2d-x 3.1
FileUtils::fullPathFilename() is used to retrieve the full file name from a specified base file name. It returns an absolute path if an existing file is found otherwise it returns the given argument.
Provided that a search path array and resolution array are specified during the initialization, the engine will combine them with the App path into a full path and try to locate a readable file within them. The details of workflow are explained below.
Assume that we are running the following codes.
FileUtils::getInstance()->addSearchPath("Images1"); FileUtils::getInstance()->addSearchPath("Images2"); FileUtils::getInstance()->addSearchResolutionsOrder("iPad"); FileUtils::getInstance()->addSearchResolutionsOrder("iPhone"); std::string fullpath = FileUtils::getInstance()->fullPathForFilename("sprite.png");
And we are placing some files inside Resource directory like this.
Resource\Images1\iPad\house.png Resource\Images1\iPhone\house.png Resource\Images2\iPad\house.png Resource\Images2\iPad\sprite.png Resource\Images2\iPhone\house.png Resource\Images2\iPhone\sprite.png
So the engine will do a search in the following order:
Resource\Images1\iPad\sprite.png Resource\Images1\iPhone\sprite.png Resource\Images2\iPad\sprite.png Resource\Images2\iPhone\sprite.png
In our case, line #3 will be hit since an existing file is found on device. And in practice you don't have to care about how and where the resource files are packed. You just use
FileUtils::fullPathForFilename() to pick a proper file among those you placed in the Resource directory before.