cocos2d-x lua 示例Demo

具体代码:

 

新建之后,你首先看到的main.lua启动到MyApp.lua。

 

view source print ?
1. require("app.MyApp"):create():run()

看MyApp.lua文件:
1、require("app.MyApp")
这里执行的MyApp.lua的代码是:

 

 

view source print ?
1. local MyApp = class("MyApp", cc.load("mvc").AppBase) -- 继承cc.mvc.AppBase
2.  
3. function MyApp:onCreate()
4. math.randomseed(os.time())
5. end
6.  
7. return MyApp
2、require("app.MyApp").create()
MyApp.create()执行后,执行的代码是:
view source print ?
1. function MyApp:ctor()
2. MyApp.super.ctor(self)
3. end
为什么create()了之后会执行MyApp:ctor()?请看function.lua下的function class(classname, super)方法:
view source print ?
01. cls.new = function(...)
02. local instance
03. if cls.__create then
04. instance = cls.__create(...)
05. else
06. instance = {}
07. end
08. setmetatableindex(instance, cls)
09. instance.class = cls
10. instance:ctor(...)
11. return instance
12. end
13. cls.create = function(_, ...)
14. return cls.new(...)
15. end

可以看到,在class的实现方法里面,给每个创建的类声明了一个new()方法,方法里面调用了ctor()构造方法(ctor只是个名字,所以不是有些人认为的create了之后,当然会调用构造方法,lua没有类,只是我们模仿了类)

 

3.require("app.MyApp").create():run()

 

view source print ?
1. function AppBase:run(initSceneName)
2. initSceneName = initSceneName or self.configs_.defaultSceneName
3. self:enterScene(initSceneName)
4. end

其实:self.configs_.defaultSceneName = “MainScene”,就是进入初始界面。

 

对于MyApp.lua文件,如果我修改成下面的样子,是不是你就理解了上面所做的事情:

 

view source print ?
01. local AppBase = class("AppBase")
02.  
03. -- 初始化调用
04. function AppBase:ctor(configs)
05. self.configs_ = {
06. viewsRoot  = "app.views",
07. modelsRoot = "app.models",
08. defaultSceneName = "MainScene",
09. }
10.  
11. for k, v in pairs(configs or {}) do
12. self.configs_[k] = v
13. end
14.  
15. if type(self.configs_.viewsRoot) ~= "table" then
16. self.configs_.viewsRoot = {self.configs_.viewsRoot}
17. end
18. if type(self.configs_.modelsRoot) ~= "table" then
19. self.configs_.modelsRoot = {self.configs_.modelsRoot}
20. end
21.  
22. if DEBUG > 1 then
23. dump(self.configs_, "AppBase configs")
24. end
25.  
26. if CC_SHOW_FPS then
27. cc.Director:getInstance():setDisplayStats(true)
28. end
29.  
30. -- event
31. self:onCreate()
32. end
33.  
34. -- 运行场景
35. function AppBase:run(initSceneName)
36. initSceneName = initSceneName or self.configs_.defaultSceneName
37. self:enterScene(initSceneName)
38. end
39.  
40. -- 选择场景
41. function AppBase:enterScene(sceneName, transition, time, more)
42. local view = self:createView(sceneName)
43. view:showWithScene(transition, time, more)
44. return view
45. end
46.  
47. -- 选择UI,即是view
48. function AppBase:createView(name)
49. for _, root in ipairs(self.configs_.viewsRoot) do
50. local packageName = string.format("%s.%s", root, name)
51. local status, view = xpcall(function()
52. return require(packageName)
53. end, function(msg)
54. if not string.find(msg, string.format("'%s' not found:", packageName)) then
55. print("load view error: ", msg)
56. end
57. end)
58. local t = type(view)
59. if status and (t == "table" or t == "userdata") then
60. return view:create(self, name)
61. end
62. end
63. error(string.format("AppBase:createView() - not found view "%s" in search paths "%s"",
64. name, table.concat(self.configs_.viewsRoot, ",")), 0)
65. end
66.  
67. -- 创建时调用
68. function AppBase:onCreate()
69. end
70.  
71. return AppBase
MainScene.lua

 

view source print ?
01. local MainScene = class("MainScene", cc.load("mvc").ViewBase)
02.  
03. function MainScene:onCreate()
04. -- add background image
05. display.newSprite("MainSceneBg.jpg")
06. :move(display.center)
07. :addTo(self)
08.  
09. -- add play button
10. local playButton = cc.MenuItemImage:create("PlayButton.png", "PlayButton.png")
11. :onClicked(function()
12. self:getApp():enterScene("PlayScene")   -- 进入游戏界面
13. end)
14. cc.Menu:create(playButton)
15. :move(display.cx, display.cy - 200)
16. :addTo(self)
17. end
18.  
19. return MainScene

你可能感兴趣的:(lua开发,游戏开发,cocos2d-x,cocos2d-x,lua)