在 cocos2d-x 2.x 中的 AssetsManager 包中提供了一個 CreateDirectory 方法。這個方法可以跨平臺支持創(chuàng)建文件夾。在實際項目中運行沒有問題。
bool AssetsManager::createDirectory(const char *path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
mode_t processMask = umask(0);
int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
umask(processMask);
if (ret != 0 (errno != EEXIST))
{
return false;
}
return true;
#else
BOOL ret = CreateDirectoryA(path, NULL);
if (!ret ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
}
return true;
#endif
}
在 cocos2d-x 2.x 的 AssetsManager sample 范例中提供了一個 reset 方法,這個方法使用系統(tǒng)命令遞歸刪除文件夾。
void UpdateLayer::reset(cocos2d::CCObject *pSender)
{
pProgressLabel->setString(" ");
// Remove downloaded files
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
string command = "rm -r ";
// Path may include space.
command += "\"" + pathToSave + "\"";
system(command.c_str());
#else
string command = "rd /s /q ";
// Path may include space.
command += "\"" + pathToSave + "\"";
system(command.c_str());
#endif
// Delete recorded version codes.
getAssetsManager()->deleteVersion();
createDownloadedDir();
}
The iOS Simulator libSystem was initialized out of order. This is most often caused by running host executables or inserting host dylibs. In the future, this will cause an abort.
require("lfs")
function os.exists(path)
return CCFileUtils:sharedFileUtils():isFileExist(path)
end
function os.mkdir(path)
if not os.exists(path) then
return lfs.mkdir(path)
end
return true
end
function os.rmdir(path)
print("os.rmdir:", path)
if os.exists(path) then
local function _rmdir(path)
local iter, dir_obj = lfs.dir(path)
while true do
local dir = iter(dir_obj)
if dir == nil then break end
if dir ~= "." and dir ~= ".." then
local curDir = path..dir
local mode = lfs.attributes(curDir, "mode")
if mode == "directory" then
_rmdir(curDir.."/")
elseif mode == "file" then
os.remove(curDir)
end
end
end
local succ, des = os.remove(path)
if des then print(des) end
return succ
end
_rmdir(path)
end
return true
end
上面的代碼在 iOS 模擬器和 Android 真機上測試成功。Windows系統(tǒng)、Mac OSX 以及 iOS 真機還沒有測試。我測試后會立即更新。