需要来自可写路径的Lua文件
我现在被这个问题困扰了大约3天 我在游戏中使用cocos2d-x2.1.3和Lua脚本。 使用需要来自可写路径的Lua文件,lua,cocos2d-x,Lua,Cocos2d X,我现在被这个问题困扰了大约3天 我在游戏中使用cocos2d-x2.1.3和Lua脚本。 使用AssetManager作为基础,我现在已成功地从服务器获取.zip文件并将其加载到应用程序中。然而,有一个小小的陷阱 在我的项目中(基于AssetManagerTest),它下载了一个.zip文件(称为package.zip),其中包含两个文件,一个.png(称为HelloWorld.png)和一个.lua文件(称为game.lua).lua文件只是创建一个HelloWorld场景,只是为了测试它是否
AssetManager
作为基础,我现在已成功地从服务器获取.zip
文件并将其加载到应用程序中。然而,有一个小小的陷阱
在我的项目中(基于AssetManagerTest
),它下载了一个.zip
文件(称为package.zip
),其中包含两个文件,一个.png
(称为HelloWorld.png
)和一个.lua
文件(称为game.lua
).lua文件只是创建一个HelloWorld
场景,只是为了测试它是否有效,它确实有效
然后我编辑了game.lua
文件,需要另一个.lua
文件,名为gem.lua
。当我尝试运行它时,它报告一个错误,说它找不到gem.lua
。因此我尝试编辑我的C++
代码,以利用CCLuaEngine::addSearchPath(const char*)
,但它仍然不起作用
我下载package.zip
文件夹并将其解压缩到CCFileUtils::getWritablePath()
返回的路径中。文件被正确解压缩,找到了游戏.lua
。当我尝试将gem.lua
添加到包中时,我添加了以下代码:
// Obtain the full script path. The value returned is:
// CCFileUtils::getWritablePath() + "/games/game/scripts"
const std::string scriptPath = MyAssetManager::sharedManager() -> getScriptPath();
// Obtain the script file path
std::string gamePath = scriptPath + "/game.lua";
// Add the scriptPath to the LuaEngine search paths
CCLuaEngine::defaultEngine() -> addSearchPath( scriptPath.c_str() );
// Run the script
CCLuaEnging::defaultEngine() -> executeScriptFile( gamePath.c_str() );
即使gem.lua
位于正确的路径(/games/game/scripts/
),引擎也无法找到它。环顾四周后,发现搜索路径的基本路径是资源
文件夹
有没有办法让CCLuaEngine
查找可写路径?不过,这可以通过资源来完成
这是我收到的全部错误消息:
[LUA ERROR] ...D35-DB7A1B515ED3/Documents/games/game/scripts/game.lua:3: module 'gem.lua' not found:
no field package.preload['gem.lua']gem.lua
no file './gem/lua.lua'
no file '/usr/local/share/luajit-2.0.1/gem/lua.lua'
no file '/usr/local/share/lua/5.1/gem/lua.lua'
no file '/usr/local/share/lua/5.1/gem/lua/init.lua'
no file 'Scripts/gem/lua.lua'
no file 'Scripts/Common/gem/lua.lua'
no file 'Scripts/HomeScene/gem/lua.lua'
no file 'Scripts/Games/gem/lua.lua'
no file '/Users/xxxx/Library/Application Support/iPhone Simulator/6.1/Applications/E0CFD097-5DED-4E6D-8D35-DB7A1B515ED3/Documents/games/game/scripts/gem/lua.lua'
no file './gem/lua.so'
no file '/usr/local/lib/lua/5.1/gem/lua.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
no file './gem.so'
no file '/usr/local/lib/lua/5.1/gem.so'
no file '/usr/local/lib/lua/5.1/loadall.so'
考虑到错误消息,您可能在代码中编写了类似于
require“gem.lua”
的内容。您应该删除扩展名:require“gem”
您得到的错误消息是什么?(表示找不到gem.lua
)编辑了问题并添加了错误消息。谢谢。这修复了上面的错误,但cocos2d-x仍然无法找到它。我通过将脚本文件夹添加到资源搜索路径,而不是lua搜索路径来解决它。谢谢!