
使用3.4python新建一个lua工程,进去一看,我去,这个都是个啥!!!仔细研究,在研究,终于看懂了一点,先记下来。
程序入口,main.m 无变化,AppDelegate::applicationDIDFinishLaunching()初始化luaEngine,executeScriptfile("src/main.lua”)执行脚本
ok,然后听我慢慢道来
先上目录结构
main.lua
cc.fileUtils:getInstance():setPopupNotify(false)cc.fileUtils:getInstance():addSearchPath("src/")cc.fileUtils:getInstance():addSearchPath("res/")--加载配置文件require "config"--加载cocos quick 大概require "cocos.init"local function main() require("app.MyApp"):create():run()endlocal status,msg = xpcall(main,__G__TRACKBACK__)if not status then print(msg)end MyApp.lua
local MyApp = class("MyApp",cc.load("mvc").AppBase)function MyApp:onCreate() math.randomseed(os.time())endreturn MyApp
ok到这里,我晕了,这个是啥情况!!!没有scene:create,没有layer:create(),这是要闹哪样?
经过漫长的探索,终于找到了原因,要像理解这段代码,首先要说一说
local MyApp = class("MyApp",cc.load("mvc").AppBase)
这是之前没注意的地方,猜测是quick里面的东西。functions.lua里面写了不少有意思的方法,建议大家看看
我们找到里面的class方法,上代码
function class(classname,...) local cls = {__cname = classname} local supers = {...} for _,super in ipairs(supers) do local superType = type(super) if superType == "function" then cls.__create = super elseif superType == "table" then if super[".isclass"] then cls.__create = function() return super:create() end else cls.__supers = cls.__supers or {} cls.__supers[#cls.__supers + 1] = super if not cls.super then cls.super = super end end end end cls.__index = cls if not cls.__supers or #cls.__supers == 1 then setMetatable(cls,{__index = cls.super}) else setMetatable(cls,{__index = function(_,key) local supers = cls.__supers for i = 1,#supers do local super = supers[i] if super[key] then return super[key] end end end}) end if not cls.ctor then cls.ctor = function() end end cls.new = function(...) local instance if cls.__create then instance = cls.__create(...) else instance = {} end setMetatableindex(instance,cls) instance.class = cls instance:ctor(...) return instance end cls.create = function(_,...) return cls.new(...) end return clsend简单点说class的目的就是实现继承,我们知道lua继承通过__index的方式。class(a,b)的结果就是a继承b,在create的时候调用a:ctor(…)。 我们dump出MyApp
[LUA-print] dump from: [string "app/MyApp.lua"]:4: in main chunk
[LUA-print] - "<var>" = {
[LUA-print] - "__cname" = "MyApp"
[LUA-print] - "__index" = *REF*
[LUA-print] - "__supers" = {
[LUA-print] - 1 = {
[LUA-print] - "__cname" = "AppBase"
[LUA-print] - "__index" = *REF*
[LUA-print] - "create" = function: 0x7fd36ecaf9d0
[LUA-print] - "createVIEw" = function: 0x7fd36ecafba0
[LUA-print] - "ctor" = function: 0x7fd36ecaf8b0
[LUA-print] - "enterScene" = function: 0x7fd36ecafb70
[LUA-print] - "new" = function: 0x7fd36ecaefd0
[LUA-print] - "onCreate" = function: 0x7fd36ecafbd0
[LUA-print] - "run" = function: 0x7fd36ecafb40
[LUA-print] - }
[LUA-print] - }
[LUA-print] - "create" = function: 0x7fd36bd9d380
[LUA-print] - "new" = function: 0x7fd36bd9cc40
[LUA-print] - "super" = *REF*
[LUA-print] - }
ok我们回头在看main.lua的代码就明白了,其实是调用
<span ><strong>AppBase</strong></span>:create():run()
function AppBase:run(initScenename)
initScenename = initScenename or self.configs_.defaultScenename
self:enterScene(initScenename)
end
好了,这下总算弄清了。
然后我发现mvc这个目录,应该不止就这点用处,敢叫mvc必然提供了一种3层编程结构。仔细打量,果然更多的功能是在BaseVIEw.lua里
--继承ccnodelocal VIEwBase = class("VIEwBase",cc.Node)--create时调用function VIEwBase:ctor(app,name) self:enableNodeEvents() self.app_ = app self.name_ = name -- check CSB resource file -- cocostudio支持 local res = rawget(self.class,"RESOURCE_filename") if res then self:createResoueceNode(res) end -- 不详 local binding = rawget(self.class,"RESOURCE_BINDING") if res and binding then self:createResoueceBinding(binding) end if self.onCreate then self:onCreate() endendfunction VIEwBase:createResoueceNode(resourcefilename)endfunction VIEwBase:createResoueceBinding(binding)endreturn VIEwBase显而易见,在BaseVIEw.lua中添加了对于cocostudio等编译器的支持。 然后我们在看一下一直被忽略的问题,既然游戏中新建层继承mvc,mvc是何时加载的呐?
翻看cocos.framework.package_supportcc.load发现在这里require mvc目录下的lua文件,然后
require "cocos.init"追到cocos.init
require "cocos.cocos2d.Cocos2d"require "cocos.cocos2d.Cocos2dConstants"require "cocos.cocos2d.functions"if CC_USE_FRAMEWORK then require "cocos.framework.init"else 。。。。end
继续追。。。。
找到这句
require("cocos.framework.package_support")
总结以上是内存溢出为你收集整理的cocos2dx 3.4 lua mvc代码解析全部内容,希望文章能够帮你解决cocos2dx 3.4 lua mvc代码解析所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)