如何在cocos2d-x上的目录中列出文件?

时间:2014-08-07 08:11:27

标签: cocos2d-x

我将文件保存在iPhone模拟器的可写目录中。 可以在运行时动态删除和创建文件。 我想使用目录路径而不是文件名列出特定目录中的这些文件。

示例:

/Users/mine/Library/Application Support/iPhone Simulator/7.1/Applications/022F7891-1983-4791-AB59-451B2EAF4FB8/Documents/stage/stage_1.json
/Users/mine/Library/Application Support/iPhone Simulator/7.1/Applications/022F7891-1983-4791-AB59-451B2EAF4FB8/Documents/stage/stage_2.json
/Users/mine/Library/Application Support/iPhone Simulator/7.1/Applications/022F7891-1983-4791-AB59-451B2EAF4FB8/Documents/stage/stage_24.json
/Users/mine/Library/Application Support/iPhone Simulator/7.1/Applications/022F7891-1983-4791-AB59-451B2EAF4FB8/Documents/stage/stage_3.json
/Users/mine/Library/Application Support/iPhone Simulator/7.1/Applications/022F7891-1983-4791-AB59-451B2EAF4FB8/Documents/stage/stage_4.bmp
  

vector = getFilesInDir(FileUtils :: getInstance() - > getWritablePath()+ string(" Documents / stage")," json");

     

vector => (" stage_1.json"," stage_1.json"," stage_1.json"," stage_1.json")

1 个答案:

答案 0 :(得分:2)

cocos2dx中没有用于列出目录内容的跨平台API。您可以在创建文件名时将其存储在数组中。

如果您只想按名称阅读文件,则需要在CCFileUtils

中插入搜索路径的文件夹路径以搜索路径
FileUtils *fileUtils = FileUtils::getInstance();
vector<string> searchPaths = fileUtils->getSearchPaths();
string path = fileUtils->getWritablePath() + "stage";
searchPaths.insert(searchPaths.begin(), path);
fileUtils->setSearchPaths(searchPaths);