具體代碼:
新建之後,你首先看到的main.lua啓動到MyApp.lua。
1.
require(
"app.MyApp"
):create():run()
看MyApp.lua文件:
1、require("app.MyApp")
這裏執行的MyApp.lua的代碼是:
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
MyApp.create()執行後,執行的代碼是:
1.
function MyApp:ctor()
2.
MyApp.
super
.ctor(self)
3.
end
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()
1.
function AppBase:run(initSceneName)
2.
initSceneName = initSceneName or self.configs_.defaultSceneName
3.
self:enterScene(initSceneName)
4.
end
其實:self.configs_.defaultSceneName = “MainScene”,就是進入初始界面。
對於MyApp.lua文件,如果我修改成下面的樣子,是不是你就理解了上面所做的事情:
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
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