3、cocos2d-Lua的运行流程与场景



打开工程根目录下的配置文件config.json:
{
"init_cfg": {
"isLandscape": true,
"isWindowTop": false,
"name": "redDefense",
"width": 1920,
"height": 1080,
"entry": "src/main.lua",
"consolePort": 6050,
"uploadPort": 6060,
"debugPort": 10000,
"forwardConsolePort": 10089,
"forwardUploadPort": 10091
},
"simulator_screen_size": [
{
"title": "iPhone 3Gs (480x320)",
"width": 480,
"height": 320
},
{
"title": "iPhone 4 (960x640)",
"width": 960,
"height": 640
},
{
"title": "iPhone 5 (1136x640)",
"width": 1136,
"height": 640
},
{
"title": "iPad (1024x768)",
"width": 1024,
"height": 768
},
{
"title": "iPad Retina (2048x1536)",
"width": 2048,
"height": 1536
},
{
"title": "Android (800x480)",
"width": 800,
"height": 480
},
{
"title": "Android (854x480)",
"width": 854,
"height": 480
},
{
"title": "Android (1280x720)",
"width": 1280,
"height": 720
},
{
"title": "Android (1920x1080)",
"width": 1920,
"height": 1080
}
]
}
可以看到 "entry": "src/main.lua",也就是说入口文件是main.lua,进而打开main.lua:
cc.FileUtils:getInstance():setPopupNotify(false)
cc.FileUtils:getInstance():addSearchPath("src/")
cc.FileUtils:getInstance():addSearchPath("res/")

require "config"
require "cocos.init"

local function main()
require("app.MyApp"):create():run()
end

local status, msg = xpcall(main, __G__TRACKBACK__)
if not status then
print(msg)
end
执行main函数,main函数里加载MyApp创建并运行,进而打开MyApp.lua:
local MyApp = class("MyApp", cc.load("mvc").AppBase)

function MyApp:onCreate()
math.randomseed(os.time())
end

return MyApp
这里就有点看头,MyApp仅仅是继承自AppBase,onCreate函数只是初始化了下随机数种子,也就意味着更多的操作在AppBase中,我们打开分析:
local AppBase = class("AppBase")

function AppBase:ctor(configs)
self.configs_ = {
viewsRoot = "app.views",
modelsRoot = "app.models",
defaultSceneName = "MainScene",
}

for k, v in pairs(configs or {}) do
self.configs_[k] = v
end

if type(self.configs_.viewsRoot) ~= "table" then
self.configs_.viewsRoot = {self.configs_.viewsRoot}
end
if type(self.configs_.modelsRoot) ~= "table" then
self.configs_.modelsRoot = {self.configs_.modelsRoot}
end

if DEBUG > 1 then
dump(self.configs_, "AppBase configs")
end

if CC_SHOW_FPS then
cc.Director:getInstance():setDisplayStats(true)
end

-- event
self:onCreate()
end

function AppBase:run(initSceneName)
initSceneName = initSceneName or self.configs_.defaultSceneName
self:enterScene(initSceneName)
end

function AppBase:enterScene(sceneName, transition, time, more)
local view = self:createView(sceneName)
view:showWithScene(transition, time, more)
return view
end

function AppBase:createView(name)
for _, root in ipairs(self.configs_.viewsRoot) do
local packageName = string.format("%s.%s", root, name)
local status, view = xpcall(function()
return require(packageName)
end, function(msg)
if not string.find(msg, string.format("'%s' not found:", packageName)) then
print("load view error: ", msg)
end
end)
local t = type(view)
if status and (t == "table" or t == "userdata") then
return view:create(self, name)
end
end
error(string.format("AppBase:createView() - not found view \"%s\" in search paths \"%s\"",
name, table.concat(self.configs_.viewsRoot, ",")), 0)
end

function AppBase:onCreate()
end

return AppBase
在前面的分析中知道main.lua是执行的是App的run函数,作为基类的AppBase,当然也要被调用run函数,因此直接看run函数:主要是创建并进入场景initSceneName,如果run的参数没有指定开始的场景则使用默认场景defaultSceneName,默认场景在构造函数的时候被初始化为MainScene,也就是说场景默认将从MainScene开始。

如果给run指定了场景名(字符串),那么项目启动后会直接进入该场景,这点有个好处是如果要调试设计某场景可以直接从这个场景进入,不必从其他场景进入了。也就是在main.lua中这么调用即可:
local function main()
require("app.MyApp"):create():run('PlayScene')
end
那么项目启动后会直接进入PlayScene场景,而不再是默认的MainScene场景。

我们现在不做改动,仍然接着默认流程分析, MainScene为主场景, 创建并显示一张背景图,创建显示一个“Play”按钮,按钮的点击事件是进入PlayScene场景。

local  MainScene  class ( "MainScene" cc . load ( "mvc" ). ViewBase )
function MainScene:onCreate()
-- add background image
display.newSprite("MainSceneBg.jpg")
:move(display.center)
:addTo(self)

-- add play button
local playButton = cc.MenuItemImage:create("PlayButton.png", "PlayButton.png")
:onClicked(function()
self:getApp():enterScene("PlayScene")
end)
cc.Menu:create(playButton)
:move(display.cx, display.cy - 200)
:addTo(self)
end

return MainScene


PlayScene场景:
local PlayScene = class("PlayScene", cc.load("mvc").ViewBase)

local GameView = import(".GameView")

function PlayScene:onCreate()
-- create game view and add it to stage
self.gameView_ = GameView:create()
:addEventListener(GameView.events.PLAYER_DEAD_EVENT, handler(self, self.onPlayerDead))
:start()
:addTo(self)
end

function PlayScene:onPlayerDead(event)
-- add game over text
local text = string.format("You killed %d bugs", self.gameView_:getKills())
cc.Label:createWithSystemFont(text, "Arial", 96)
:align(display.CENTER, display.center)
:addTo(self)

-- add exit button
local exitButton = cc.MenuItemImage:create("ExitButton.png", "ExitButton.png")
:onClicked(function()
self:getApp():enterScene("MainScene")
end)
cc.Menu:create(exitButton)
:move(display.cx, display.cy - 200)
:addTo(self)
end

return PlayScene
PlayScene场景创建游戏逻辑视图GameView并调用start函数开始游戏,绑定了一个游戏结束的事件,这个事件会在GameView中在触发游戏结束逻辑时发生,PlayScene由于绑定了该事件,则会在游戏结束时调用其绑定的回调事件,以显示战绩和分数,显示一个退出按钮,退出按钮事件为进入MainScene场景。

至此,场景的切换已经很清晰了,那么主要的游戏逻辑便是在GameView中了。

打开工程根目录下的配置文件config.json:
{
"init_cfg": {
"isLandscape": true,
"isWindowTop": false,
"name": "redDefense",
"width": 1920,
"height": 1080,
"entry": "src/main.lua",
"consolePort": 6050,
"uploadPort": 6060,
"debugPort": 10000,
"forwardConsolePort": 10089,
"forwardUploadPort": 10091
},
"simulator_screen_size": [
{
"title": "iPhone 3Gs (480x320)",
"width": 480,
"height": 320
},
{
"title": "iPhone 4 (960x640)",
"width": 960,
"height": 640
},
{
"title": "iPhone 5 (1136x640)",
"width": 1136,
"height": 640
},
{
"title": "iPad (1024x768)",
"width": 1024,
"height": 768
},
{
"title": "iPad Retina (2048x1536)",
"width": 2048,
"height": 1536
},
{
"title": "Android (800x480)",
"width": 800,
"height": 480
},
{
"title": "Android (854x480)",
"width": 854,
"height": 480
},
{
"title": "Android (1280x720)",
"width": 1280,
"height": 720
},
{
"title": "Android (1920x1080)",
"width": 1920,
"height": 1080
}
]
}
可以看到 "entry": "src/main.lua",也就是说入口文件是main.lua,进而打开main.lua:
cc.FileUtils:getInstance():setPopupNotify(false)
cc.FileUtils:getInstance():addSearchPath("src/")
cc.FileUtils:getInstance():addSearchPath("res/")

require "config"
require "cocos.init"

local function main()
require("app.MyApp"):create():run()
end

local status, msg = xpcall(main, __G__TRACKBACK__)
if not status then
print(msg)
end
执行main函数,main函数里加载MyApp创建并运行,进而打开MyApp.lua:
local MyApp = class("MyApp", cc.load("mvc").AppBase)

function MyApp:onCreate()
math.randomseed(os.time())
end

return MyApp
这里就有点看头,MyApp仅仅是继承自AppBase,onCreate函数只是初始化了下随机数种子,也就意味着更多的操作在AppBase中,我们打开分析:
local AppBase = class("AppBase")

function AppBase:ctor(configs)
self.configs_ = {
viewsRoot = "app.views",
modelsRoot = "app.models",
defaultSceneName = "MainScene",
}

for k, v in pairs(configs or {}) do
self.configs_[k] = v
end

if type(self.configs_.viewsRoot) ~= "table" then
self.configs_.viewsRoot = {self.configs_.viewsRoot}
end
if type(self.configs_.modelsRoot) ~= "table" then
self.configs_.modelsRoot = {self.configs_.modelsRoot}
end

if DEBUG > 1 then
dump(self.configs_, "AppBase configs")
end

if CC_SHOW_FPS then
cc.Director:getInstance():setDisplayStats(true)
end

-- event
self:onCreate()
end

function AppBase:run(initSceneName)
initSceneName = initSceneName or self.configs_.defaultSceneName
self:enterScene(initSceneName)
end

function AppBase:enterScene(sceneName, transition, time, more)
local view = self:createView(sceneName)
view:showWithScene(transition, time, more)
return view
end

function AppBase:createView(name)
for _, root in ipairs(self.configs_.viewsRoot) do
local packageName = string.format("%s.%s", root, name)
local status, view = xpcall(function()
return require(packageName)
end, function(msg)
if not string.find(msg, string.format("'%s' not found:", packageName)) then
print("load view error: ", msg)
end
end)
local t = type(view)
if status and (t == "table" or t == "userdata") then
return view:create(self, name)
end
end
error(string.format("AppBase:createView() - not found view \"%s\" in search paths \"%s\"",
name, table.concat(self.configs_.viewsRoot, ",")), 0)
end

function AppBase:onCreate()
end

return AppBase
在前面的分析中知道main.lua是执行的是App的run函数,作为基类的AppBase,当然也要被调用run函数,因此直接看run函数:主要是创建并进入场景initSceneName,如果run的参数没有指定开始的场景则使用默认场景defaultSceneName,默认场景在构造函数的时候被初始化为MainScene,也就是说场景默认将从MainScene开始。

如果给run指定了场景名(字符串),那么项目启动后会直接进入该场景,这点有个好处是如果要调试设计某场景可以直接从这个场景进入,不必从其他场景进入了。也就是在main.lua中这么调用即可:
local function main()
require("app.MyApp"):create():run('PlayScene')
end
那么项目启动后会直接进入PlayScene场景,而不再是默认的MainScene场景。

我们现在不做改动,仍然接着默认流程分析, MainScene为主场景, 创建并显示一张背景图,创建显示一个“Play”按钮,按钮的点击事件是进入PlayScene场景。

local  MainScene  class ( "MainScene" cc . load ( "mvc" ). ViewBase )
function MainScene:onCreate()
-- add background image
display.newSprite("MainSceneBg.jpg")
:move(display.center)
:addTo(self)

-- add play button
local playButton = cc.MenuItemImage:create("PlayButton.png", "PlayButton.png")
:onClicked(function()
self:getApp():enterScene("PlayScene")
end)
cc.Menu:create(playButton)
:move(display.cx, display.cy - 200)
:addTo(self)
end

return MainScene


PlayScene场景:
local PlayScene = class("PlayScene", cc.load("mvc").ViewBase)

local GameView = import(".GameView")

function PlayScene:onCreate()
-- create game view and add it to stage
self.gameView_ = GameView:create()
:addEventListener(GameView.events.PLAYER_DEAD_EVENT, handler(self, self.onPlayerDead))
:start()
:addTo(self)
end

function PlayScene:onPlayerDead(event)
-- add game over text
local text = string.format("You killed %d bugs", self.gameView_:getKills())
cc.Label:createWithSystemFont(text, "Arial", 96)
:align(display.CENTER, display.center)
:addTo(self)

-- add exit button
local exitButton = cc.MenuItemImage:create("ExitButton.png", "ExitButton.png")
:onClicked(function()
self:getApp():enterScene("MainScene")
end)
cc.Menu:create(exitButton)
:move(display.cx, display.cy - 200)
:addTo(self)
end

return PlayScene
PlayScene场景创建游戏逻辑视图GameView并调用start函数开始游戏,绑定了一个游戏结束的事件,这个事件会在GameView中在触发游戏结束逻辑时发生,PlayScene由于绑定了该事件,则会在游戏结束时调用其绑定的回调事件,以显示战绩和分数,显示一个退出按钮,退出按钮事件为进入MainScene场景。

至此,场景的切换已经很清晰了,那么主要的游戏逻辑便是在GameView中了。

你可能感兴趣的:(cocos)