lua-tests

来源:互联网 发布:win10用看图软件 编辑:程序博客网 时间:2024/06/05 08:28
本篇博客打算从研究Cocos2d-x引擎提供的测试例子来写起,笔者针对Cocos2d-x 3.1.1这个版本来介绍如何来学习它给我们提供的例子,通过这些例子来深入学习Cocos2d-x核心API的使用。在学习过程中,笔者同样跟很多初学者一样有很多疑惑,为了能帮助到更多初学者,笔者会提出自己的疑惑然后进行解决,笔者会把每一个例子都进行详细说明,对相关代码进行注释,那么以后初学者就可以对照笔者的博客来学习Cocos2d-x的使用,我想会事半功倍的。对笔者来说,什么版本并不重要,重要的是学习的思路,接下来的系列博客就是笔者学习Cocos2d-x的思路。
  如何开始呢?
  自然先到Cocos2d-x官网下载相应版本的引擎,笔者这里是Cocos2d-x 3.1.1
  下载完成后,希望童鞋们自己试着去搭建环境,网络和笔者前面的博客均有介绍,笔者在这里就不多说了。
  最好要在win32平台、Android平台都运行成功过HelloWorld,有了这个基础之后,就可以找例子学习了。

 Cocos2d-x 3.1.1例子的代码在cocos2d-x-3.1.1\tests下:
 
这里就有我们的C++代码和Lua代码,我们要学习的是Lua项目,怎么运行这些项目呢,看下图:

双击使用Visual Studio打开,笔者这里是2012的。
  
右键设置lua-tests为启动项目,然后进行编译运行,成功之后的效果如下:
从这些例子,我们可以了解到Cocos2d-x所有相关API所能实现的基础效果,笔者认为没有任何学习资料能比得上这些例子了。

怎么查看实现以上效果的Lua代码呢,下面笔者会给大家介绍。

笔者用到的一个开发工具是LDT,童鞋们可以到这里下载:http://www.eclipse.org/koneki/ldt/
打开LDT,切换工作空间到我们的lua-tests项目中,如下:


然后新建Lua项目,取名为src,这样我们就可以把当前目录下src所有的Lua代码都包含进来了:

这样我们就可以很方便查看每一个例子的Lua代码,查看它的具体实现。

我们的Lua项目的入口在哪里呢?
打开win32项目,找到AppDelegate.cpp,打开查看:
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "cocos2d.h"  
  2. #include "AppDelegate.h"  
  3. #include "CCLuaEngine.h"  
  4. #include "audio/include/SimpleAudioEngine.h"  
  5. #include "lua_assetsmanager_test_sample.h"  
  6.   
  7. using namespace CocosDenshion;  
  8.   
  9. USING_NS_CC;  
  10.   
  11. AppDelegate::AppDelegate()  
  12. {  
  13. }  
  14.   
  15. AppDelegate::~AppDelegate()  
  16. {  
  17.     SimpleAudioEngine::end();  
  18. }  
  19.   
  20. bool AppDelegate::applicationDidFinishLaunching()  
  21. {  
  22.     // 获得导演类实例  
  23.     auto director = Director::getInstance();  
  24.     // 获取渲染所有东西的 EGLView NA NA  
  25.     auto glview = director->getOpenGLView();  
  26.     if(!glview) {  
  27.         glview = GLView::createWithRect("Lua Tests", Rect(0,0,900,640));  
  28.         director->setOpenGLView(glview);  
  29.     }  
  30.   
  31.     // turn on display FPS  
  32.     // 打开显示帧屏  
  33.     director->setDisplayStats(true);  
  34.   
  35.     // set FPS. the default value is 1.0/60 if you don't call this  
  36.     // 设置游戏画面每秒显示的帧数,默认是60帧。  
  37.     director->setAnimationInterval(1.0 / 60);  
  38.   
  39.     // 获得屏幕大小  
  40.     auto screenSize = glview->getFrameSize();  
  41.   
  42.     // 获得设计大小  
  43.     auto designSize = Size(480, 320);  
  44.   
  45.     if (screenSize.height > 320)  
  46.     {  
  47.         auto resourceSize = Size(960, 640);  
  48.         director->setContentScaleFactor(resourceSize.height/designSize.height);  
  49.     }  
  50.   
  51.     // 设置屏幕设计分辨率  
  52.     glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::FIXED_HEIGHT);  
  53.   
  54.     // register lua engine  
  55.     LuaEngine* pEngine = LuaEngine::getInstance();  
  56.     ScriptEngineManager::getInstance()->setScriptEngine(pEngine);  
  57.   
  58. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID ||CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_MAC)  
  59.     LuaStack* stack = pEngine->getLuaStack();  
  60.     register_assetsmanager_test_sample(stack->getLuaState());  
  61. #endif  
  62.     // 执行脚本语言  
  63.     pEngine->executeScriptFile("src/controller.lua");  
  64.   
  65.     return true;  
  66. }  
  67.   
  68. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too  
  69. void AppDelegate::applicationDidEnterBackground()  
  70. {  
  71.     Director::getInstance()->stopAnimation();  
  72.   
  73.     SimpleAudioEngine::getInstance()->pauseBackgroundMusic();  
  74. }  
  75.   
  76. // this function will be called when the app is active again  
  77. void AppDelegate::applicationWillEnterForeground()  
  78. {  
  79.     Director::getInstance()->startAnimation();  
  80.   
  81.     SimpleAudioEngine::getInstance()->resumeBackgroundMusic();  
  82. }  

我们会发现代码中这么一段:
pEngine->executeScriptFile("src/controller.lua");

这个就是我们的入口文件,我们执行Lua的代码是从controller.lua这个文件开始的。

我们用LDT查看一下它的代码:
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -- avoid memory leak  
  2. -- 避免内存泄漏  
  3. collectgarbage("setpause", 100)   
  4. collectgarbage("setstepmul", 5000)  
  5.       
  6. require "src/mainMenu"  
  7. ----------------  
  8.   
  9.   
  10. -- run  
  11.   
  12. local glView = cc.Director:getInstance():getOpenGLView()  
  13. local screenSize = glView:getFrameSize()  
  14. local designSize = {width = 480, height = 320}  
  15. local fileUtils = cc.FileUtils:getInstance()  
  16.   
  17. if screenSize.height > 320 then  
  18.     local searchPaths = {}  
  19.     table.insert(searchPaths, "hd")  
  20.     fileUtils:setSearchPaths(searchPaths)  
  21. end  
  22.   
  23. local targetPlatform = cc.Application:getInstance():getTargetPlatform()  
  24. local resPrefix = ""  
  25. if cc.PLATFORM_OS_IPAD  == targetPlatform or cc.PLATFORM_OS_IPHONE == targetPlatform or cc.PLATFORM_OS_MAC == targetPlatform then  
  26.     resPrefix = ""  
  27. else  
  28.     resPrefix = "res/"  
  29. end  
  30.   
  31. local searchPaths = fileUtils:getSearchPaths()  
  32. table.insert(searchPaths, 1, resPrefix)  
  33. table.insert(searchPaths, 1, resPrefix .. "cocosbuilderRes")  
  34.   
  35. if screenSize.height > 320 then  
  36.     table.insert(searchPaths, 1, resPrefix .. "hd")  
  37.     table.insert(searchPaths, 1, resPrefix .. "ccs-res")  
  38.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd")  
  39.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/Images")  
  40.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/ArmatureComponentTest")  
  41.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/AttributeComponentTest")  
  42.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/BackgroundComponentTest")  
  43.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/EffectComponentTest")  
  44.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/LoadSceneEdtiorFileTest")  
  45.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/ParticleComponentTest")  
  46.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/SpriteComponentTest")  
  47.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/TmxMapComponentTest")  
  48.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/UIComponentTest")  
  49.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/hd/scenetest/TriggerTest")  
  50. else  
  51.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/Images")  
  52.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/ArmatureComponentTest")  
  53.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/AttributeComponentTest")  
  54.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/BackgroundComponentTest")  
  55.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/EffectComponentTest")  
  56.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/LoadSceneEdtiorFileTest")  
  57.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/ParticleComponentTest")  
  58.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/SpriteComponentTest")  
  59.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/TmxMapComponentTest")  
  60.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/UIComponentTest")  
  61.     table.insert(searchPaths, 1, resPrefix .. "ccs-res/scenetest/TriggerTest")  
  62. end  
  63.   
  64. fileUtils:setSearchPaths(searchPaths)  
  65.   
  66. -- 创建场景  
  67. local scene = cc.Scene:create()  
  68. -- 添加菜单  
  69. scene:addChild(CreateTestMenu())  
  70. if cc.Director:getInstance():getRunningScene() then  
  71.     cc.Director:getInstance():replaceScene(scene)  
  72. else  
  73.     -- 根据给定的场景进入 Director的主循环 只能调用他运行你的第一个场景.  
  74.     cc.Director:getInstance():runWithScene(scene)  
  75. end  

然后我们就可以根据这个,来理清整个测试项目的脉路,我们也许想知道,例子中的那些菜单是怎么显示出来的,例子中又是如何进行切换场景的。只要我们知道如何开始了,以后我们就可以慢慢分析每一个例子所给我提供的代码。

在controller.lua文件中引入了maniMenu.lua
通过这个语句:require "src/mainMenu"
我们来看看mainMenu的代码:
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. -- 引入资源文件  
  2. require "Cocos2d"  
  3. require "Cocos2dConstants"  
  4. require "Opengl"  
  5. require "OpenglConstants"  
  6. require "StudioConstants"  
  7. require "GuiConstants"  
  8. require "src/helper"  
  9. require "src/testResource"  
  10. require "src/VisibleRect"  
  11.   
  12. require "src/AccelerometerTest/AccelerometerTest"  
  13. require "src/ActionManagerTest/ActionManagerTest"  
  14. require "src/ActionsEaseTest/ActionsEaseTest"  
  15. require "src/ActionsProgressTest/ActionsProgressTest"  
  16. require "src/ActionsTest/ActionsTest"  
  17. require "src/AssetsManagerTest/AssetsManagerTest"  
  18. require "src/BugsTest/BugsTest"  
  19. require "src/ClickAndMoveTest/ClickAndMoveTest"  
  20. require "src/CocosDenshionTest/CocosDenshionTest"  
  21. require "src/CocoStudioTest/CocoStudioTest"  
  22. require "src/CurrentLanguageTest/CurrentLanguageTest"  
  23. require "src/DrawPrimitivesTest/DrawPrimitivesTest"  
  24. require "src/EffectsTest/EffectsTest"  
  25. require "src/EffectsAdvancedTest/EffectsAdvancedTest"  
  26. require "src/ExtensionTest/ExtensionTest"  
  27. require "src/FontTest/FontTest"  
  28. require "src/IntervalTest/IntervalTest"  
  29. require "src/KeypadTest/KeypadTest"  
  30. require "src/LabelTest/LabelTest"  
  31. require "src/LabelTestNew/LabelTestNew"  
  32. require "src/LayerTest/LayerTest"  
  33. require "src/MenuTest/MenuTest"  
  34. require "src/MotionStreakTest/MotionStreakTest"  
  35. require "src/NewEventDispatcherTest/NewEventDispatcherTest"  
  36. require "src/NodeTest/NodeTest"  
  37. require "src/OpenGLTest/OpenGLTest"  
  38. require "src/ParallaxTest/ParallaxTest"  
  39. require "src/ParticleTest/ParticleTest"  
  40. require "src/PerformanceTest/PerformanceTest"  
  41. require "src/RenderTextureTest/RenderTextureTest"  
  42. require "src/RotateWorldTest/RotateWorldTest"  
  43. require "src/Sprite3DTest/Sprite3DTest"  
  44. require "src/SpriteTest/SpriteTest"  
  45. require "src/SceneTest/SceneTest"  
  46. require "src/SpineTest/SpineTest"  
  47. require "src/Texture2dTest/Texture2dTest"  
  48. require "src/TileMapTest/TileMapTest"  
  49. require "src/TouchesTest/TouchesTest"  
  50. require "src/TransitionsTest/TransitionsTest"  
  51. require "src/UserDefaultTest/UserDefaultTest"  
  52. require "src/ZwoptexTest/ZwoptexTest"  
  53. require "src/LuaBridgeTest/LuaBridgeTest"  
  54. require "src/XMLHttpRequestTest/XMLHttpRequestTest"  
  55. require "src/PhysicsTest/PhysicsTest"  
  56.   
  57. -- 行间距  
  58. local LINE_SPACE = 40  
  59.   
  60. -- 当前位置  
  61. local CurPos = {x = 0, y = 0}  
  62. -- 开始位置  
  63. local BeginPos = {x = 0, y = 0}  
  64.   
  65. -- 定义一张表  
  66. local _allTests = {  
  67.   { isSupported = true,  name = "Accelerometer"          , create_func=             AccelerometerMain  },  
  68.   { isSupported = true,  name = "ActionManagerTest"      , create_func   =         ActionManagerTestMain  },  
  69.   { isSupported = true,  name = "ActionsEaseTest"        , create_func   =           EaseActionsTest      },  
  70.   { isSupported = true,  name = "ActionsProgressTest"    , create_func   =       ProgressActionsTest      },  
  71.   { isSupported = true,  name = "ActionsTest"            , create_func   =               ActionsTest      },  
  72.   { isSupported = true,  name = "AssetsManagerTest"      , create_func   =         AssetsManagerTestMain      },  
  73.   { isSupported = false,  name = "Box2dTest"              , create_func=                 Box2dTestMain  },  
  74.   { isSupported = false,  name = "Box2dTestBed"           , create_func=              Box2dTestBedMain  },  
  75.   { isSupported = true,  name = "BugsTest"               , create_func=              BugsTestMain      },  
  76.   { isSupported = false,  name = "ChipmunkAccelTouchTest" , create_func=    ChipmunkAccelTouchTestMain  },  
  77.   { isSupported = true,  name = "ClickAndMoveTest"       , create_func   =          ClickAndMoveTest      },  
  78.   { isSupported = true,  name = "CocosDenshionTest"      , create_func   =         CocosDenshionTestMain  },  
  79.   { isSupported = true,  name = "CocoStudioTest"         , create_func   =         CocoStudioTestMain  },  
  80.   { isSupported = false,  name = "CurlTest"               , create_func=                  CurlTestMain  },  
  81.   { isSupported = true,  name = "CurrentLanguageTest"    , create_func=   CurrentLanguageTestMain      },  
  82.   { isSupported = true,  name = "DrawPrimitivesTest"     , create_func=        DrawPrimitivesTest      },  
  83.   { isSupported = true,  name = "EffectsTest"            , create_func   =               EffectsTest      },  
  84.   { isSupported = true,  name = "EffectAdvancedTest"     , create_func   =        EffectAdvancedTestMain  },  
  85.   { isSupported = true,  name = "ExtensionsTest"         , create_func=        ExtensionsTestMain      },  
  86.   { isSupported = true,  name = "FontTest"               , create_func   =              FontTestMain      },  
  87.   { isSupported = true,  name = "IntervalTest"           , create_func   =              IntervalTestMain  },  
  88.   { isSupported = true,  name = "KeypadTest"             , create_func=                KeypadTestMain  },  
  89.   { isSupported = true,  name = "LabelTest"              , create_func   =                 LabelTest      },  
  90.   { isSupported = true,  name = "LabelTestNew"           , create_func   =                 LabelTestNew      },  
  91.   { isSupported = true,  name = "LayerTest"              , create_func   =                 LayerTestMain  },  
  92.   { isSupported = true,  name = "LuaBridgeTest"          , create_func   =        LuaBridgeMainTest },  
  93.   { isSupported = true,  name = "MenuTest"               , create_func   =                  MenuTestMain  },  
  94.   { isSupported = true,  name = "MotionStreakTest"       , create_func   =          MotionStreakTest      },  
  95.   { isSupported = false,  name = "MutiTouchTest"          , create_func=          MutiTouchTestMain     },  
  96.   { isSupported = true,  name = "NewEventDispatcherTest"  , create_func   =       NewEventDispatcherTest },  
  97.   { isSupported = true,  name = "NodeTest"               , create_func   =                  CocosNodeTest },  
  98.   { isSupported = true,   name = "OpenGLTest"             , create_func=          OpenGLTestMain     },  
  99.   { isSupported = true,  name = "ParallaxTest"           , create_func   =              ParallaxTestMain  },  
  100.   { isSupported = true,  name = "ParticleTest"           , create_func   =              ParticleTest      },  
  101.   { isSupported = true,  name = "PerformanceTest"        , create_func=           PerformanceTestMain  },  
  102.   { isSupported = true,  name = "PhysicsTest"            , create_func =          PhysicsTest  },  
  103.   { isSupported = true,  name = "RenderTextureTest"      , create_func   =         RenderTextureTestMain  },  
  104.   { isSupported = true,  name = "RotateWorldTest"        , create_func   =           RotateWorldTest      },  
  105.   { isSupported = true,  name = "SceneTest"              , create_func   =                 SceneTestMain  },  
  106.   { isSupported = true,  name = "SpineTest"              , create_func   =                 SpineTestMain  },  
  107.   { isSupported = false,  name = "SchdulerTest"           , create_func=              SchdulerTestMain  },  
  108.   { isSupported = false,  name = "ShaderTest"             , create_func=            ShaderTestMain      },  
  109.   { isSupported = true,  name = "Sprite3DTest"           , create_func   =                Sprite3DTest    },  
  110.   { isSupported = true,  name = "SpriteTest"             , create_func   =                SpriteTest      },  
  111.   { isSupported = false,  name = "TextInputTest"          , create_func=             TextInputTestMain  },  
  112.   { isSupported = true,  name = "Texture2DTest"          , create_func   =             Texture2dTestMain  },  
  113.   { isSupported = false,  name = "TextureCacheTest"       , create_func=      TextureCacheTestMain      },  
  114.   { isSupported = true,  name = "TileMapTest"            , create_func   =               TileMapTestMain  },  
  115.   { isSupported = true,  name = "TouchesTest"            , create_func   =               TouchesTest      },  
  116.   { isSupported = true,  name = "TransitionsTest"        , create_func   =           TransitionsTest      },  
  117.   { isSupported = true,  name = "UserDefaultTest"        , create_func=           UserDefaultTestMain  },  
  118.   { isSupported = true,  name = "XMLHttpRequestTest"     , create_func   =        XMLHttpRequestTestMain  },  
  119.   { isSupported = true,  name = "ZwoptexTest"            , create_func   =               ZwoptexTestMain  }  
  120. }  
  121.   
  122. local TESTS_COUNT = table.getn(_allTests)  
  123.   
  124. -- create scene 创建场景  
  125. local function CreateTestScene(nIdx)  
  126.   cc.Director:getInstance():purgeCachedData()  
  127.   local scene = _allTests[nIdx].create_func()  
  128.   return scene  
  129. end  
  130. -- create menu 创建菜单  
  131. function CreateTestMenu()  
  132.   -- 菜单层  
  133.   local menuLayer = cc.Layer:create()  
  134.   
  135.   local function closeCallback()  
  136.     -- 结束执行,释放正在运行的场景。   
  137.     cc.Director:getInstance():endToLua()  
  138.   end  
  139.     
  140.   -- 菜单回调  
  141.   local function menuCallback(tag)  
  142.     print(tag)  
  143.     local Idx = tag - 10000  
  144.     local testScene = CreateTestScene(Idx)  
  145.     if testScene then  
  146.       -- 切换场景  
  147.       cc.Director:getInstance():replaceScene(testScene)  
  148.     end  
  149.   end  
  150.   
  151.   -- add close menu 添加关闭菜单  
  152.   local s = cc.Director:getInstance():getWinSize()  
  153.   local CloseItem = cc.MenuItemImage:create(s_pPathClose, s_pPathClose)  
  154.   CloseItem:registerScriptTapHandler(closeCallback)  
  155.   CloseItem:setPosition(cc.p(s.width - 30, s.height - 30))  
  156.     
  157.   local CloseMenu = cc.Menu:create()  
  158.   CloseMenu:setPosition(0, 0)  
  159.   CloseMenu:addChild(CloseItem)  
  160.   menuLayer:addChild(CloseMenu)  
  161.   -- 获取目标平台  
  162.   local targetPlatform = cc.Application:getInstance():getTargetPlatform()  
  163.   if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) then  
  164.     CloseMenu:setVisible(false)  
  165.   end  
  166.   
  167.   -- add menu items for tests  
  168.   -- 添加菜单项  
  169.   local MainMenu = cc.Menu:create()  
  170.   local index = 0  
  171.   local obj = nil  
  172.   for index, obj in pairs(_allTests) do  
  173.     -- 创建文本(obj.name 为文本名称, s_arialPath为字体,24为字体大小)  
  174.     local testLabel = cc.Label:createWithTTF(obj.name, s_arialPath, 24)  
  175.     -- 设置锚点  
  176.     testLabel:setAnchorPoint(cc.p(0.5, 0.5))  
  177.     -- 创建菜单项标签  
  178.     local testMenuItem = cc.MenuItemLabel:create(testLabel)  
  179.     -- 如果此项不支持,则设置菜单项不可用  
  180.     if not obj.isSupported then  
  181.       testMenuItem:setEnabled(false)  
  182.     end  
  183.     -- 注册脚本处理句柄  
  184.     testMenuItem:registerScriptTapHandler(menuCallback)  
  185.     -- 设置菜单标签显示的位置,设置到居中的位置  
  186.     testMenuItem:setPosition(cc.p(s.width / 2, (s.height - (index) * LINE_SPACE)))  
  187.     -- 添加菜单项到菜单中去  
  188.     MainMenu:addChild(testMenuItem, index + 10000, index + 10000)  
  189.   end  
  190.     
  191.   -- 设置菜单内容大小  
  192.   MainMenu:setContentSize(cc.size(s.width, (TESTS_COUNT + 1) * (LINE_SPACE)))  
  193.   MainMenu:setPosition(CurPos.x, CurPos.y)  
  194.   -- 将菜单添加到层中去  
  195.   menuLayer:addChild(MainMenu)  
  196.   
  197.   -- handling touch events  
  198.   -- 处理触摸事件  
  199.   local function onTouchBegan(touch, event)  
  200.     -- 获取触摸点的位置  
  201.     BeginPos = touch:getLocation()  
  202.     -- CCTOUCHBEGAN event must return true  
  203.     return true  
  204.   end  
  205.   
  206.   -- 手指移动时触发  
  207.   local function onTouchMoved(touch, event)  
  208.     local location = touch:getLocation()  
  209.     local nMoveY = location.y - BeginPos.y  
  210.     local curPosx, curPosy = MainMenu:getPosition()  
  211.     local nextPosy = curPosy + nMoveY  
  212.     local winSize = cc.Director:getInstance():getWinSize()  
  213.     if nextPosy < 0 then  
  214.       MainMenu:setPosition(0, 0)  
  215.       return  
  216.     end  
  217.       
  218.     if nextPosy > ((TESTS_COUNT + 1) * LINE_SPACE - winSize.height) then  
  219.       MainMenu:setPosition(0, ((TESTS_COUNT + 1) * LINE_SPACE - winSize.height))  
  220.       return  
  221.     end  
  222.       
  223.     --   
  224.     MainMenu:setPosition(curPosx, nextPosy)  
  225.     BeginPos = {x = location.x, y = location.y}  
  226.     CurPos = {x = curPosx, y = nextPosy}  
  227.   end  
  228.     
  229.   -- 创建事件监听器  
  230.   local listener = cc.EventListenerTouchOneByOne:create()  
  231.   -- 注册事件监听  
  232.   listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )  
  233.   listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )  
  234.   -- 获取事件派发器  
  235.   local eventDispatcher = menuLayer:getEventDispatcher()  
  236.   eventDispatcher:addEventListenerWithSceneGraphPriority(listener, menuLayer)  
  237.   
  238.   return menuLayer  
  239. end  

我们就可以发现,这里就是界面中菜单的实现,mainMenu中还引入了其他文件,比如testResource.lua
[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. s_pPathGrossini       = "Images/grossini.png"  
  2. s_pPathSister1        = "Images/grossinis_sister1.png"  
  3. s_pPathSister2        = "Images/grossinis_sister2.png"  
  4. s_pPathB1             = "Images/b1.png"  
  5. s_pPathB2             = "Images/b2.png"  
  6. s_pPathR1             = "Images/r1.png"  
  7. s_pPathR2             = "Images/r2.png"  
  8. s_pPathF1             = "Images/f1.png"  
  9. s_pPathF2             = "Images/f2.png"  
  10. s_pPathBlock          = "Images/blocks.png"  
  11. s_back                = "Images/background.png"  
  12. s_back1               = "Images/background1.png"  
  13. s_back2               = "Images/background2.png"  
  14. s_back3               = "Images/background3.png"  
  15. s_stars1              = "Images/stars.png"  
  16. s_stars2              = "Images/stars2.png"  
  17. s_fire                = "Images/fire.png"  
  18. s_snow                = "Images/snow.png"  
  19. s_streak              = "Images/streak.png"  
  20. s_PlayNormal          = "Images/btn-play-normal.png"  
  21. s_PlaySelect          = "Images/btn-play-selected.png"  
  22. s_AboutNormal         = "Images/btn-about-normal.png"  
  23. s_AboutSelect         = "Images/btn-about-selected.png"  
  24. s_HighNormal          = "Images/btn-highscores-normal.png"  
  25. s_HighSelect          = "Images/btn-highscores-selected.png"  
  26. s_Ball                = "Images/ball.png"  
  27. s_Paddle              = "Images/paddle.png"  
  28. s_pPathClose          = "Images/close.png"  
  29. s_MenuItem            = "Images/menuitemsprite.png"  
  30. s_SendScore           = "Images/SendScoreButton.png"  
  31. s_PressSendScore      = "Images/SendScoreButtonPressed.png"  
  32. s_Power               = "Images/powered.png"  
  33. s_AtlasTest           = "Images/atlastest.png"  
  34.   
  35. -- tilemaps resource  
  36. s_TilesPng            = "TileMaps/tiles.png"  
  37. s_LevelMapTga         = "TileMaps/levelmap.tga"  
  38.   
  39. -- spine test resource  
  40. s_pPathSpineBoyJson       = "spine/spineboy.json"  
  41. s_pPathSpineBoyAtlas       = "spine/spineboy.atlas"  
  42.   
  43. -- fonts resource  
  44. s_markerFeltFontPath   = "fonts/Marker Felt.ttf"  
  45. s_arialPath            = "fonts/arial.ttf"  
  46. s_thonburiPath         = "fonts/Thonburi.ttf"  
  47. s_tahomaPath           = "fonts/tahoma.ttf"  

这个文件定义了所有的资源路径,一些图片、json资源、字体资源等。

其他相关的文件,笔者在这里就不贴代码,这个大家私下去查看,我在这里只是提供相关的思路,让大家理清如何使用Cocos2d-x给我们提供的例子进行学习。

下一篇博客,笔者将会介绍第一个例子-重力加速器,非常感谢你的关注。
0 0