Open \ cocos2d-x-2.2.3 \ cocos2d-win32.vc2012.slnsln inside there is a TestLua project preliminary study completed... (don't tease) start the project, cocos2d-x demo are in this project, good, start into the question. The project entry is: AppDelegate. cpp, AppDelegate: applicationDidFinishLaunching ()
Bool AppDelegate: applicationDidFinishLaunching () {// initialize the director class ccctor ctor * pDirector = CCDirector: sharedDirector (); pDirector-> setOpenGLView ()); // enable display FPS pDirector-> setDisplayStats (true); // set FPS. the default value is 1.0/60. If not, modify pDirector-> setAnimationInterval (1.0/60); // register Lua engine CCLuaEngine * pEngine = CCLuaEngine: defaultEngine (); CCScriptEngineManager :: sharedManager ()-> setScriptEngine (pEngine); CCLuaStack * pStack = pEngine-> getLuaStack (); lua_State * tolua_s = pStack-> getLuaState (); then (tolua_s ); // perform special processing on ios, win32, and android platforms # 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
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/Participant lecomponenttest"); 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 ); // here is the entry file for executing lua-> executeScriptFile ("luasloud/controller. lua "); return true ;}
Read the following row by row. The last row is the lua script called in TestLua. Let's take a closer look at what controller. lua has done.
-- avoid memory leakcollectgarbage("setpause", 100) collectgarbage("setstepmul", 5000)require "luaScript/mainMenu"------------------ runlocal scene = CCScene:create()scene:addChild(CreateTestMenu())CCDirector:sharedDirector():runWithScene(scene)
1. The garbage collection parameter is set in controller. lua.
2. Create a scenario
3. Add the returned value of CreateTestMenu () to the scenario class to run the scenario. The CreateTestMenu () method is in mainMenu. lua
Go to mainMenu. lua and check it out.
1. First, require a bunch of files
2. There is a local Variable _ allTests. This table is the main menu for constructing the demo of TestLua. (Lua uses tables to construct the desired structure. This is one of the most powerful parts)
3. What do I do in CreateTestMenu?
There are two callback functions for creating a set: closeCallBack and menuCallBack. Create and register the two menu items and add them to the menu to create the main menu according to _ allTest, add to menuLayer and register two script processors. The closeCallBack callback is used to handle the close button event. menuCallBack processes the Click Event of the menu item:
local Idx = tag - 10000
local testScene = CreateTestScene(Idx) if testScene then
-- Replacement scenario CCDirector: shareddire(): replaceScene (testScene) end
local function CreateTestScene(nIdx)
-- Use a subscript to call the corresponding function local scene = _ allTests [nIdx]. create_func () -- clear the cache CCDirector: shareddire(): purgeCachedData () return scene end
MenuCallBack is mainly used to locate the function to be called by subscript to create a scenario.
The creation of the main interface ends here. Switching the field class depends on the Creation function triggered accordingly.