【问题标题】:cocos2d-x run a lua script twicecocos2d-x 运行一个lua脚本两次
【发布时间】:2013-09-25 16:48:10
【问题描述】:

我正在开发一个 cocos2d-x 应用程序。 该应用程序从用 C++ 编写的主要场景开始。 主场景将创建一个用 lua 编写的逻辑场景。 以下是主场景中的代码:

void CMainSceneLayer::menuSelectedCallBack(cocos2d::CCObject * pSender)
{
    std::string scriptFile = "luaScripts/LogicScene.lua";
    scriptFile = CCFileUtils::sharedFileUtils()->fullPathForFilename(scriptFile.c_str());
    CCLog("scriptFile: %s\n", scriptFile.c_str());
    CCLuaEngine::defaultEngine()->executeScriptFile(scriptFile.c_str());
}

以下案例是在安卓手机中运行应用

当它运行“LogicScene.lua”时,一切都按预期进行。 当它从 Logic 场景返回到 MainScene 时,它​​也很顺利。 但是当主场景再次尝试运行“LogicScene.lua”时,什么也没有发生。 它只保留在主要场景中。 LogicScene.lua 没有日志输出。

我通过编写一个只打印日志的 test.lua 对其进行了测试。 主场景第一次运行 test.lua 时,logCat 中显示了一条日志。 但是当 test.lua 运行第二次时,什么都没有发生。

我使用数千个关键字在 Google 上搜索了数千次,但没有找到任何有用的信息。

令人惊讶的是,当我在 Windows 上测试时,一切都很好。

PS,在android set上,所有脚本都放在“assets/luaScripts/” 并且 LogicScene.lua 需要“assets/luaScripts/”中的一些其他 lua 脚本。

而在windows上,所有脚本都放在“Resources/luaScripts/”中

怎么了?有人可以帮忙吗?

【问题讨论】:

    标签: android c++ lua cocos2d-x


    【解决方案1】:

    我已经找到了解决这个问题的方法。

    对于安卓版本,

    CCLuaEngine::defaultEngine()->executeScriptFile(arg) 它使用 require 语句来执行 arg。

    所以当 CCLuaEngine::defaultEngine()->executeScriptFile 在同一个 lua 文件上第二次或第四次调用时,什么都不会发生。

    我修改了 lua/cocos2dx_support/CCLuaStack.cpp 中的源文件来解决这个问题。

    我在这里粘贴我的解决方案的代码:

    int CCLuaStack::executeScriptFile(const char* filename)
    {
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
        //std::string code("require \""); //origin code
        std::string code(std::string("package.loaded[\"") + std::string(filename) + std::string("\"] = nil\n")); //this line was added by me
        code.append("require \"");
        code.append(filename);
        code.append("\"");
        return executeString(code.c_str());
    #else
    //more codes...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2013-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多