HelloLua与HelloCpp的区别

HelloCppHelloLua的区别

HelloCpp工程的AppDelegate

bool AppDelegate::applicationDidFinishLaunching() { 
    ..... 
    // create a scene. it's an autorelease object 
    CCScene *pScene = HelloWorld::scene(); 
    // run
    pDirector->runWithScene(pScene); 
    return true;
}

改为


HelloLua工程的AppDelegate

bool AppDelegate::applicationDidFinishLaunching() {
        ..... 
    // register lua engine 注册lua引擎
    CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();                 // 1. 通过LuaEngine的静态函数获取一个Lua脚本引擎的实例对象指针
    CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);    // 2. 获取单例脚本引擎的实例对象指针  然后将Lua脚本引擎的实例对象指针设为脚本引擎管理器当前进行管理的脚本引擎

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)                                            // 如果是安卓平台,编译这个
    CCString* pstrFileContent = CCString::createWithContentsOfFile("Myhello.lua");
    if (pstrFileContent)
    {
        pEngine->executeString(pstrFileContent->getCString());
    }
#else
    std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath("Myhello.lua");    // 获取文件hello.lua的全部路径,因为该文件在资源文件夹内,直接写名字就行
    pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());                        // 将这个路径的目录放入到脚本引擎的搜索目录
    pEngine->executeScriptFile(path.c_str());                                                      // 3. 执行这个路径所指向的Lua文件
#endif
        return true;
}

然后在lua文件里创建场景与运行

    -- run
    local sceneGame = CCScene:create()
    sceneGame:addChild(createLayerFarm())
    sceneGame:addChild(createLayerMenu())
    CCDirector:sharedDirector():runWithScene(sceneGame)




HelloLua的AppDelegate源文件与TestLua的区别

HelloLua工程

#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)                                            // 如果是安卓平台,编译这个
    CCString* pstrFileContent = CCString::createWithContentsOfFile("Myhello.lua");
    if (pstrFileContent)
    {
        pEngine->executeString(pstrFileContent->getCString());
    }
#else
    std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(<strong>"Myhello.lua"</strong>);    // 获取文件hello.lua的全部路径,因为该文件在资源文件夹内,直接写名字就行
    pEngine->addSearchPath(path.substr(0, <strong>path.find_last_of("/")</strong>).c_str());                        // 将这个路径的目录放入到脚本引擎的搜索目录
    pEngine->executeScriptFile(path.c_str());                                                      // 3. 执行这个路径所指向的Lua文件
#endif


TestLua工程

std::string <strong>dirPath = "luaScript"</strong>;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
    CCString* pstrFileContent = CCString::createWithContentsOfFile((dirPath + "/controller.lua").c_str());
    if (pstrFileContent)
    {
        pEngine->executeString(pstrFileContent->getCString());
    }
#else
    std::string path = CCFileUtils::sharedFileUtils()->fullPathFromRelativePath(<strong>(dirPath + "/controller.lua").c_str()</strong>);
    pEngine->addSearchPath(path.substr(0, <strong>path.find_last_of("/") - dirPath.length()</strong>).c_str());
    pEngine->executeScriptFile(path.c_str());
#endif




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值