标签:table 结束 android平台 UI one load ios pre uil
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)
2.创建一个场景
3.把CreateTestMenu() 的返回值加入到场景类里。执行场景。CreateTestMenu()方法是在mainMenu.lua里的
进入mainMenu.lua里看一下
1.首先require一堆文件
2.有一个局部变量_allTests。这个table是构造出TestLua的demo的主菜单。(lua通过表来构造自己想要的结构。这是强大的地方之中的一个)
3.CreateTestMenu里做些什么事呢?
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主要为了通过下标来找到相应要调用的函数来创建场景。
标签:table 结束 android平台 UI one load ios pre uil
原文地址:http://www.cnblogs.com/yxysuanfa/p/6789045.html