cocos2dx 3.4 lua mvc代码解析

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.4 lua mvc代码解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

使用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()
end

local 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())
end

return 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 cls
end
简单点说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 style="font-family: Menlo; font-size: 11px; "><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里

--继承ccnode
local 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() end
end
function ViewBase:createResoueceNode(resourceFilename)
end

function ViewBase:createResoueceBinding(binding)
end
return 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")

猜你在找的Cocos2d-x相关文章