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

3、cocos2d-Lua的运行流程与场景,第1张

概述打开工程根目录下的配置文件config.json: { "init_cfg": { "isLandscape": true, "isWindowTop": false, "name": "redDefense", "width": 1920, "height": 1080, "entry": "src/main.lua", "consolePort": 6050, "uploadPort": 60

打开工程根目录下的@[email protected]
{
"init_cfg": {
"isLandscape": true,
"isWindowtop": false,122)">"@R_502_6889@": "redDefense",122)">"wIDth": 1920,122)">"height": 1080,122)">"entry": "src/main.lua",122)">"consolePort": 6050,122)">"uploadPort": 6060,122)">"deBUGPort": 10000,122)">"forwardConsolePort": 10089,122)">"forwardUploadPort": 10091
},122)">"simulator_screen_size": [
{
"Title": "iPhone 3Gs (480x320)",255)">480,255)">320
{
"iPhone 4 (960x640)",255)">960
,255)">640
"iPhone 5 (1136x640)",255)">1136,0)">"iPad (1024x768)",255)">1024,255)">768
"iPad Retina (2048x1536)",255)">2048,255)">1536
"AndroID (800x480)",255)">800,255)">480
"AndroID (854x480)",255)">854,0)">"AndroID (1280x720)",255)">1280,255)">720
"AndroID (1920x1080)",255)">1080
}
]
}
可以看到 "entry": "src/main.lua",也就是说入口文件是main.lua,进而打开main.lua:
cc.fileUtils:getInstance():setPopupNotify(false)
addSearchPath("src/")
"res/")

require "config"
"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:
MyApp = class("MyApp",43)">load("mvc").AppBase)

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

AppBase:ctor(configs)
self.configs_ = {
vIEwsRoot = "app.vIEws",
modelsRoot = "app.models",43)">defaultScene@R_502_6889@ = "MainScene",
}

for k,153)">v in pairs(configs or {}) do
configs_[k] = v
if type(configs_.vIEwsRoot) ~= "table" vIEwsRoot = {vIEwsRoot}
modelsRoot) ~= modelsRoot = {modelsRoot}
DEBUG > 1 dump(configs_,0)">"AppBase configs")
CC_SHOW_FPS Director:setdisplayStats(true)
-- event
self:onCreate()
run(initScene@R_502_6889@)
initScene@R_502_6889@ = initScene@R_502_6889@ or defaultScene@R_502_6889@
enterScene(initScene@R_502_6889@)
enterScene(scene@R_502_6889@,Transition,255)">time,255)">more)
vIEw = createVIEw(scene@R_502_6889@)
vIEw:showWithScene(vIEw
createVIEw(@R_502_6889@)
_,153)">root ipairs(vIEwsRoot) local package@R_502_6889@ = string.format("%s.%s",153)">root,153)">vIEw = function()
return require(package@R_502_6889@)
end,128)">function(msg)
if not find(msg,0)">"'%s' not found:",package@R_502_6889@)) "load vIEw error: ",128)"> end)
t = type(vIEw)
if and (t == or "userdata") return vIEw:create(self,@R_502_6889@)
end
error("AppBase:createVIEw() - not found vIEw \"%s\" in search paths \"%s\"",
@R_502_6889@,0)">table.concat(vIEwsRoot,0)">",")),255)">0)
AppBase
在前面的分析中知道main.lua是执行的是App的run函数,作为基类的AppBase,当然也要被调用run函数,因此直接看run函数:主要是创建并进入场景initScene@R_502_6889@,如果run的参数没有指定开始的场景则使用默认场景defaultScene@R_502_6889@,默认场景在构造函数的时候被初始化为MainScene,也就是说场景默认将从MainScene开始。

如果给run指定了场景名(字符串),那么项目启动后会直接进入该场景,这点有个好处是如果要调试设计某场景可以直接从这个场景进入,不必从其他场景进入了。也就是在main.lua中这么调用即可:
run('PlayScene')
end
那么项目启动后会直接进入PlayScene场景,而不再是默认的MainScene场景。
我们现在不做改动,仍然接着默认流程分析, MainScene为主场景, 创建并显示一张背景图,创建显示一个“Play”按钮,按钮的点击事件是进入PlayScene场景。
local MainScene = class ( "MainScene" , cc . load "mvc" ). VIEwBase )
MainScene:-- add background image
display.newSprite("MainSceneBg.jpg")
:move(center)
:addTo(self)

-- add play button
playbutton = MenuItemImage:create("Playbutton.png",0)">"Playbutton.png")
:onClicked(function()
self:getApp():enterScene("PlayScene")
end)
Menu:playbutton)
:cx,43)">cy - 200)
:self)
MainScene


PlayScene场景:
PlayScene = "PlayScene",43)">VIEwBase)

GameVIEw = import(".GameVIEw")

PlayScene:-- create game vIEw and add it to stage
gameVIEw_ = GameVIEw:create()
:addEventListener(GameVIEw.events.PLAYER_DEAD_EVENT,0)">handler(self,43)">onPlayerDead))
:start()
:onPlayerDead(event)
-- add game over text
text = "You killed %d BUGs",43)">gameVIEw_:getKills())
Label:createWithSystemFont(text,0)">"Arial",255)">96)
:align(CENTER,128)">-- add exit button
exitbutton = "Exitbutton.png",0)">"Exitbutton.png")
:"MainScene")
exitbutton)
:PlayScene
PlayScene场景创建游戏逻辑视图GameVIEw并调用start函数开始游戏,绑定了一个游戏结束的事件,这个事件会在GameVIEw中在触发游戏结束逻辑时发生,PlayScene由于绑定了该事件,则会在游戏结束时调用其绑定的回调事件,以显示战绩和分数,显示一个退出按钮,退出按钮事件为进入MainScene场景。
至此,场景的切换已经很清晰了,那么主要的游戏逻辑便是在GameVIEw中了。
打开工程根目录下的@[email protected]
"init_cfg": {
"isLandscape": true,122); Font-weight:bold">"isWindowtop": false,122); Font-weight:bold">"@R_502_6889@": "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)">}
]
}
可以看到 "entry": "src/main.lua",也就是说入口文件是main.lua,进而打开main.lua:
false)
"src/")
"res/")

"config"
"cocos.init"

local function "app.MyApp"):end

local if not then
end
执行main函数,main函数里加载MyApp创建并运行,进而打开MyApp.lua:
"MyApp",0); Font-weight:bold">"mvc").function return MyApp
这里就有点看头,MyApp仅仅是继承自AppBase,onCreate函数只是初始化了下随机数种子,也就意味着更多的 *** 作在AppBase中,我们打开分析:
"AppBase")

"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(package@R_502_6889@)
end,128); Font-weight:bold">function(if not "'%s' not found:",package@R_502_6889@)) "load vIEw error: ",128); Font-weight:bold"> end)
if and (or "userdata") return vIEw:create(self,@R_502_6889@)
end
"AppBase:createVIEw() - not found vIEw \"%s\" in search paths \"%s\"",0); Font-weight:bold">",")),153)">AppBase
在前面的分析中知道main.lua是执行的是App的run函数,作为基类的AppBase,当然也要被调用run函数,因此直接看run函数:主要是创建并进入场景initScene@R_502_6889@,如果run的参数没有指定开始的场景则使用默认场景defaultScene@R_502_6889@,默认场景在构造函数的时候被初始化为MainScene,也就是说场景默认将从MainScene开始。

如果给run指定了场景名(字符串),那么项目启动后会直接进入该场景,这点有个好处是如果要调试设计某场景可以直接从这个场景进入,不必从其他场景进入了。也就是在main.lua中这么调用即可:
'PlayScene')
end
那么项目启动后会直接进入PlayScene场景,而不再是默认的MainScene场景。
我们现在不做改动,仍然接着默认流程分析, MainScene为主场景, 创建并显示一张背景图,创建显示一个“Play”按钮,按钮的点击事件是进入PlayScene场景。
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场景:
"PlayScene",0); Font-weight:bold">".GameVIEw")

-- 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
PlayScene场景创建游戏逻辑视图GameVIEw并调用start函数开始游戏,绑定了一个游戏结束的事件,这个事件会在GameVIEw中在触发游戏结束逻辑时发生,PlayScene由于绑定了该事件,则会在游戏结束时调用其绑定的回调事件,以显示战绩和分数,显示一个退出按钮,退出按钮事件为进入MainScene场景。
至此,场景的切换已经很清晰了,那么主要的游戏逻辑便是在GameVIEw中了。 总结

以上是内存溢出为你收集整理的3、cocos2d-Lua的运行流程与场景全部内容,希望文章能够帮你解决3、cocos2d-Lua的运行流程与场景所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063878.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)