1.定義一個類:
local MyApp = class("MyApp", cc.mvc.AppBase)
2.構(gòu)造函數(shù):
ctor: 構(gòu)造方法
3.調(diào)用基類構(gòu)造函數(shù):
MyApp.super.ctor(self)
4.enterScene場景跳轉(zhuǎn):
1)函數(shù)原型在AppBase中: enterScene(sceneName, args, transitionType, time, move)
函數(shù)可以接收四個參數(shù),lua語法允許少寫參數(shù),默認(rèn)為nil
sceneName是要跳轉(zhuǎn)的場景名
args是傳給該場景的構(gòu)造參數(shù),args需要是一個table
transitionType是場景切換的過度動畫類型
time是過度時間
more是過度效果附加參數(shù)
2)實(shí)際調(diào)用會執(zhí)行到如下代碼:
// 在Lua中字符串連接操作符是兩個點(diǎn) (`..´)
// lua的require將會把.轉(zhuǎn)義成路徑分割符?轉(zhuǎn)義成模塊名
local scenePackageName = self.packageRoot .. ".scenes." .. sceneName
local sceneClass = require(scenePackageName)
local scene = sceneClass.new(unpack(checktable(args)))
3)可以通過enterScene將指定的參數(shù)傳遞到指定場景的構(gòu)造函數(shù)
self:endterScene("MainScene", {“你好”, “cocos”})
MainScene:ctor(arg1, arg2)
5.不一樣的類定義?
local MainScene = class("MainScene", function()
return display.newScene("MainScene")
end)
這片代碼是定義一個名為MainScene的場景類,并且賦值給local變量
1.display.newScene("MainScene")實(shí)際會調(diào)用底層代碼創(chuàng)建一個沒有名字的場景
2.NodeEvent可以控制一組事件是否開啟監(jiān)聽
3.MainScene的最后一行是return MainScene,所以需要引用這個場景的時候值需要:
local MainScene = require("app.scenes.MainScene");
Q1.“類定義”的兩種寫法具體的意義和區(qū)別是什么?
Q2.require會不會讓被require的腳本重新執(zhí)行一次,如果是return了模塊中的lcoal變量,是不是每次返回的都將是同一個值?