打开工程根目录下的配置文件config.json:
@H_301_5@
{可以看到 @H_301_5@"entry": "src/main.lua",也就是说入口文件是main.lua,进而打开main.lua:
@H_404_10@"init_cfg": {
@H_404_10@"isLandscape": @H_404_10@true,
@H_404_10@"isWindowTop": @H_404_10@false,122)">@H_404_10@"name": @H_404_10@"redDefense",122)">@H_404_10@"width": 1920,122)">@H_404_10@"height": 1080,122)">@H_404_10@"entry": @H_404_10@"src/main.lua",122)">@H_404_10@"consolePort": 6050,122)">@H_404_10@"uploadPort": 6060,122)">@H_404_10@"debugPort": 10000,122)">@H_404_10@"forwardConsolePort": 10089,122)">@H_404_10@"forwardUploadPort": 10091
},122)">@H_404_10@"simulator_screen_size": [
{
@H_404_10@"title": @H_404_10@"iPhone 3Gs (480x320)",255)">480,255)">320
{
@H_404_10@"iPhone 4 (960x640)",255)">960,255)">640
@H_404_10@"iPhone 5 (1136x640)",255)">1136,0)">@H_404_10@"iPad (1024x768)",255)">1024,255)">768
@H_404_10@"iPad Retina (2048x1536)",255)">2048,255)">1536
@H_404_10@"Android (800x480)",255)">800,255)">480
@H_404_10@"Android (854x480)",255)">854,0)">@H_404_10@"Android (1280x720)",255)">1280,255)">720
@H_404_10@"Android (1920x1080)",255)">1080
}
]
}
cc.FileUtils:getInstance():setPopupNotify(false)执行main函数,main函数里加载MyApp创建并运行,进而打开MyApp.lua:
addSearchPath(@H_404_10@"src/")
@H_404_10@"res/")
require @H_404_10@"config"
@H_404_10@"cocos.init"
@H_404_10@
@H_404_10@local function main()
require(@H_404_10@"app.MyApp"):create():run()
@H_404_10@end
@H_404_10@
@H_404_10@local status,msg = xpcall(main,__G__TRACKBACK__)
@H_404_10@if not status @H_404_10@then
@H_404_10@print(msg)
@H_404_10@end
MyApp = class(@H_404_10@"MyApp",43)">load(@H_404_10@"mvc").AppBase)这里就有点看头,MyApp仅仅是继承自AppBase,onCreate函数只是初始化了下随机数种子,也就意味着更多的操作在AppBase中,我们打开分析:
@H_404_10@function MyApp:onCreate()
math.randomseed(os.time())
@H_404_10@return MyApp
AppBase = @H_404_10@"AppBase")在前面的分析中知道main.lua是执行的是App的run函数,作为基类的AppBase,当然也要被调用run函数,因此直接看run函数:主要是创建并进入场景initSceneName,如果run的参数没有指定开始的场景则使用默认场景defaultSceneName,默认场景在构造函数的时候被初始化为MainScene,也就是说场景默认将从MainScene开始。
AppBase:ctor(configs)
self.configs_ = {
viewsRoot = @H_404_10@"app.views",
modelsRoot = @H_404_10@"app.models",43)">defaultSceneName = @H_404_10@"MainScene",
}
@H_404_10@for k,153)">v @H_404_10@in pairs(configs @H_404_10@or {}) @H_404_10@do
@H_404_10@configs_[k] = v
@H_404_10@ if type(configs_.viewsRoot) ~= @H_404_10@"table" viewsRoot = {viewsRoot}
modelsRoot) ~= modelsRoot = {modelsRoot}
DEBUG > 1 dump(configs_,0)">@H_404_10@"AppBase configs")
CC_SHOW_FPS Director:setDisplayStats(true)
-- event
self:onCreate()
run(initSceneName)
initSceneName = initSceneName @H_404_10@or defaultSceneName
enterScene(initSceneName)
enterScene(sceneName,transition,255)">time,255)">more)
view = createView(sceneName)
view:showWithScene(view
createView(name)
_,153)">root ipairs(viewsRoot) @H_404_10@ local packageName = string.format(@H_404_10@"%s.%s",153)">root,153)">view = @H_404_10@function()
return @H_301_5@require(packageName)
@H_301_5@@H_404_10@end,128)">@H_404_10@function(msg)
@H_404_10@if not find(msg,0)">@H_404_10@"'%s' not found:",@H_301_5@@H_404_10@packageName)) @H_404_10@"load view error: ",128)">@H_404_10@ end)
t = type(view)
@H_404_10@if @H_404_10@and (t == @H_404_10@or @H_404_10@"userdata") @H_404_10@return @H_301_5@view:create(self,name)
@H_301_5@@H_404_10@ end
error(@H_404_10@"AppBase:createView() - not found view \"%s\" in search paths \"%s\"",
name,0)">table.concat(viewsRoot,0)">@H_404_10@",")),255)">0)
AppBase
如果给run指定了场景名(字符串),那么项目启动后会直接进入该场景,这点有个好处是如果要调试设计某场景可以直接从这个场景进入,不必从其他场景进入了。也就是在main.lua中这么调用即可:
@H_404_10@
MainScene:-- add background image
display.newSprite(@H_404_10@"MainSceneBg.jpg")
:move(center)
:addTo(self)
-- add play button
playButton = MenuItemImage:create(@H_404_10@"PlayButton.png",0)">@H_404_10@"PlayButton.png")
:onClicked(@H_404_10@function()
@H_301_5@@H_404_10@self:getApp():enterScene(@H_404_10@"PlayScene")
@H_404_10@end)
Menu:playButton)
:cx,43)">cy - 200)
:self)
MainScene
PlayScene = @H_404_10@"PlayScene",43)">ViewBase)PlayScene场景创建游戏逻辑视图GameView并调用start函数开始游戏,绑定了一个游戏结束的事件,这个事件会在GameView中在触发游戏结束逻辑时发生,PlayScene由于绑定了该事件,则会在游戏结束时调用其绑定的回调事件,以显示战绩和分数,显示一个退出按钮,退出按钮事件为进入MainScene场景。
GameView = import(@H_404_10@".GameView")
PlayScene:-- create game view and add it to stage
gameView_ = @H_301_5@@H_404_10@GameView:create()
:addEventListener(@H_301_5@@H_404_10@GameView.events.PLAYER_DEAD_EVENT,0)">handler(self,43)">onPlayerDead))
:start()
:onPlayerDead(event)
-- add game over text
text = @H_404_10@"You killed %d bugs",43)">gameView_:getKills())
Label:createWithSystemFont(text,0)">@H_404_10@"Arial",255)">96)
:align(CENTER,128)">-- add exit button
exitButton = @H_404_10@"ExitButton.png",0)">@H_404_10@"ExitButton.png")
:@H_404_10@"MainScene")
exitButton)
:PlayScene
至此,场景的切换已经很清晰了,那么主要的游戏逻辑便是在GameView中了。
打开工程根目录下的配置文件config.json:
@H_301_5@
"init_cfg": {可以看到 "entry": "src/main.lua",也就是说入口文件是main.lua,进而打开main.lua:
"isLandscape": true,122); font-weight:bold">"isWindowTop": false,122); font-weight:bold">"name": "redDefense",122); font-weight:bold">"width": "height": "entry": "src/main.lua",122); font-weight:bold">"consolePort": "uploadPort": "debugPort": "forwardConsolePort": "forwardUploadPort": "simulator_screen_size": [
{
"title": "iPhone 3Gs (480x320)",0); font-weight:bold">"iPhone 4 (960x640)",0); font-weight:bold">"iPhone 5 (1136x640)",0); font-weight:bold">"iPad (1024x768)",0); font-weight:bold">"iPad Retina (2048x1536)",0); font-weight:bold">"Android (800x480)",0); font-weight:bold">"Android (854x480)",0); font-weight:bold">"Android (1280x720)",0); font-weight:bold">"Android (1920x1080)",255)">}
]
}
false)执行main函数,main函数里加载MyApp创建并运行,进而打开MyApp.lua:
"src/")
"res/")
"config"
"cocos.init"
local function "app.MyApp"):end
local if not then
end
"MyApp",0); font-weight:bold">"mvc").function return MyApp这里就有点看头,MyApp仅仅是继承自AppBase,onCreate函数只是初始化了下随机数种子,也就意味着更多的操作在AppBase中,我们打开分析:
"AppBase")在前面的分析中知道main.lua是执行的是App的run函数,作为基类的AppBase,当然也要被调用run函数,因此直接看run函数:主要是创建并进入场景initSceneName,如果run的参数没有指定开始的场景则使用默认场景defaultSceneName,默认场景在构造函数的时候被初始化为MainScene,也就是说场景默认将从MainScene开始。
"app.views",0); font-weight:bold">"app.models",0); font-weight:bold">"MainScene",128); font-weight:bold">for in or {}) do
if "table" "AppBase configs")
true)
-- event
or local "%s.%s",128); font-weight:bold">function()
return require(packageName)
end,128); font-weight:bold">function(if not "'%s' not found:",packageName)) "load view error: ",128); font-weight:bold"> end)
if and (or "userdata") return view:create(self,name)
end
"AppBase:createView() - not found view \"%s\" in search paths \"%s\"",0); font-weight:bold">",")),153)">AppBase
如果给run指定了场景名(字符串),那么项目启动后会直接进入该场景,这点有个好处是如果要调试设计某场景可以直接从这个场景进入,不必从其他场景进入了。也就是在main.lua中这么调用即可:
'PlayScene')那么项目启动后会直接进入PlayScene场景,而不再是默认的MainScene场景。
end
local
MainScene
=
class
(
"MainScene"
cc
.
load
"mvc"
).
ViewBase
)
-- add background image
"MainSceneBg.jpg")
:-- add play button
"PlayButton.png",0); font-weight:bold">"PlayButton.png")
:function()
self:"PlayScene")
end)
MainScene
"PlayScene",0); font-weight:bold">".GameView")PlayScene场景创建游戏逻辑视图GameView并调用start函数开始游戏,绑定了一个游戏结束的事件,这个事件会在GameView中在触发游戏结束逻辑时发生,PlayScene由于绑定了该事件,则会在游戏结束时调用其绑定的回调事件,以显示战绩和分数,显示一个退出按钮,退出按钮事件为进入MainScene场景。
-- create game view and add it to stage
gameView_ = GameView:addEventListener(GameView.-- add game over text
"You killed %d bugs",0); font-weight:bold">"Arial",128); font-style:italic">-- add exit button
"ExitButton.png",0); font-weight:bold">"ExitButton.png")
:"MainScene")
PlayScene
至此,场景的切换已经很清晰了,那么主要的游戏逻辑便是在GameView中了。