zoukankan      html  css  js  c++  java
  • cocos2d-x:初探TestLua

    1. 打开cocos2d-x-2.2.3cocos2d-win32.vc2012.sln
    2. sln里面有个TestLuaproject
    3. 初探完成...(不要逗)
    启动一下project,cocos2d-x的demo都在这project里了,好了,開始入正题了。
    project的入口是:AppDelegate.cpp,AppDelegate::applicationDidFinishLaunching()方法里
    bool AppDelegate::applicationDidFinishLaunching()
    {
        // 初始化导演类
        CCDirector *pDirector = CCDirector::sharedDirector();
        pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
    
        // 打开 display FPS
        pDirector->setDisplayStats(true);
    
        // 设置 FPS. 默认值是1.0/60。不是的话能够改动一下
        pDirector->setAnimationInterval(1.0 / 60);
        
        // 注冊Lua引擎
        CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();
        CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
    
        CCLuaStack *pStack = pEngine->getLuaStack();
        lua_State *tolua_s = pStack->getLuaState();
        tolua_extensions_ccb_open(tolua_s);
    // 对于ios,win32和android平台做一下特殊处理
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)    
        pStack = pEngine->getLuaStack();
        tolua_s = pStack->getLuaState();
        tolua_web_socket_open(tolua_s);
    #endif
            
        std::vector<std::string> searchPaths;
        searchPaths.push_back("cocosbuilderRes");
        searchPaths.insert(searchPaths.begin(), "scenetest/ArmatureComponentTest");
        searchPaths.insert(searchPaths.begin(), "scenetest/AttributeComponentTest");
        searchPaths.insert(searchPaths.begin(), "scenetest/BackgroundComponentTest");
        searchPaths.insert(searchPaths.begin(), "scenetest/EffectComponentTest");
        searchPaths.insert(searchPaths.begin(), "scenetest/LoadSceneEdtiorFileTest");
        searchPaths.insert(searchPaths.begin(), "scenetest/ParticleComponentTest");
    	searchPaths.insert(searchPaths.begin(), "scenetest/SpriteComponentTest");
    	searchPaths.insert(searchPaths.begin(), "scenetest/TmxMapComponentTest");
    	searchPaths.insert(searchPaths.begin(), "scenetest/UIComponentTest");
    	searchPaths.insert(searchPaths.begin(), "scenetest/TriggerTest");
    
    #if CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY
        searchPaths.push_back("TestCppResources");
        searchPaths.push_back("script");
    #endif
        CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
        //这里是运行lua的入口文件
        pEngine->executeScriptFile("luaScript/controller.lua");
    
        return true;
    }

    逐行读下,最后一行是TestLua中调用到lua脚本了以下。我们细看一下controller.lua做了些什么。

    -- avoid memory leak
    collectgarbage("setpause", 100) 
    collectgarbage("setstepmul", 5000)
    	
    require "luaScript/mainMenu"
    ----------------
    
    
    -- run
    local scene = CCScene:create()
    scene:addChild(CreateTestMenu())
    CCDirector:sharedDirector():runWithScene(scene)
    

    1.controller.lua里设置了垃圾回收的參数

    2.创建一个场景

    3.把CreateTestMenu() 的返回值加入到场景类里。执行场景。CreateTestMenu()方法是在mainMenu.lua里的


    进入mainMenu.lua里看一下

    1.首先require一堆文件

    2.有一个局部变量_allTests。这个table是构造出TestLua的demo的主菜单。(lua通过表来构造自己想要的结构。这是强大的地方之中的一个)

    3.CreateTestMenu里做些什么事呢?

    • 创建布景类
    • 两个回调函数:closeCallBack和menuCallBack
    • 创建菜单,把两个菜单Item创建并注冊各自己的回调函数,把item加入到菜单
    • 依据_allTest来创建主菜单,加入到menuLayer里。并注冊两个脚本处理器
    closeCallBack回调是处理关闭button事件
    menuCallBack处理的是菜单项的点击事件:
        local Idx = tag - 10000
        local testScene = CreateTestScene(Idx)
        if testScene then
            --替换场景 
            CCDirector:sharedDirector():replaceScene(testScene)
        end
        local function CreateTestScene(nIdx)
             --通过下标来调用相应的函数
             local scene = _allTests[nIdx].create_func()
    	 --清空缓存
             CCDirector:sharedDirector():purgeCachedData()
             return scene
        end
    menuCallBack主要为了通过下标来找到相应要调用的函数来创建场景。


    主界面的创建到此结束,切换场类就要看相应触发的创建函数了。



  • 相关阅读:
    self 和 super 关键字
    NSString类
    函数和对象方法的区别
    求两个数是否互质及最大公约数
    TJU Problem 1644 Reverse Text
    TJU Problem 2520 Quicksum
    TJU Problem 2101 Bullseye
    TJU Problem 2548 Celebrity jeopardy
    poj 2586 Y2K Accounting Bug
    poj 2109 Power of Cryptography
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/6789045.html
Copyright © 2011-2022 走看看