新建之后,你首先看到的main.lua启动到MyApp.lua。
1.
require(
"app.MyApp"
):create():run()
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
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
3.require("app.MyApp").create():run()
1.
function AppBase:run(initSceneName)
2.
initSceneName = initSceneName or self.configs_.defaultSceneName
3.
self:enterScene(initSceneName)
4.
end
对于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