在lua语言中,require
语句搜寻模块有一个内置的顺序,而且能够经过package.path
来维护模块的搜索策略。
可是在cocos2d-x中,不是这样!android
cocos2d-x重载了本来的lua的require加载方式。(见Cocos2dxLuaLoader.cpp )
Cocos2dxLuaLoader逻辑的生效是在package.path以前,而且package.path在安卓上则不能很好的处理加载pkg包内部文件的问题。
因此在实际使用中,咱们只使用cocos2d-x重载的方法就能够了。ui
怎么作呢?lua
Cocos2dxLuaLoader内部是使用CCFileUtils::getFileData()
来加载lua代码的。
因此咱们要想添加本身的lua脚本搜索路径,那么只要调用CCFileUtils::addSearchPath()
就能够了。code
如下C++代码实现了在iOS和android平台上,程序先从下载路径下的scripts文件夹寻找lua文件,再从程序内置资源路径下的scripts文件夹寻找lua文件的目标。ip
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) std::string downloadedScriptPath = CCFileUtils::sharedFileUtils()->getWritablePath() + "scripts"; CCFileUtils::sharedFileUtils()->addSearchPath(downloadedScriptPath.c_str()); std::string scriptPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("scripts"); CCFileUtils::sharedFileUtils()->addSearchPath(scriptPath.c_str()); #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) std::string downloadedScriptPath = CCFileUtils::sharedFileUtils()->getWritablePath() + "/scripts"; CCFileUtils::sharedFileUtils()->addSearchPath(downloadedScriptPath.c_str()); CCFileUtils::sharedFileUtils()->addSearchPath("assets/scripts"); #elif (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) //TODO #else _ERROR_ #endif