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