一直比較關注Quick Lua,但是項目中一直使用的公司自有的Lua框架,所以一直沒機會在實際中使用下Quick Lua。看到羣裏很多人都在用這個,我在這裏梳理下開始使用的流程吧,我主要是說下實際使用上的流程問題。
比如很多學習者甚至不知道enterScene("MainScene") 爲什麼裏面可以是個字符串?當然如果你已經很熟悉框架了,這篇文章就可以跳過了,呵呵。
下面開始吧!
一、前置準備
1、安裝下載之類的,官方論壇寫的很清楚了,我就不說了。http://wiki.quick-x.com/doku.php?id=zh_cn:get_started_create_new_project2、關於IDE,我使用的IEDA,配置導出的api代碼提示,還是挺方便的。http://wiki.quick-x.com/doku.php?id=zh_cn:get_started_install_intellij_idea
二、新建一個工程
新建之後,你首先看到的main.lua啓動到MyApp.lua。
1
|
require( "app.MyApp" ). new ():run() |
1、require("app.MyApp")
這裏執行的MyApp.lua的代碼是:
1
2
|
local
MyApp = class ( "MyApp" ,
cc.mvc.AppBase) -- 繼承cc.mvc.AppBase return MyApp |
2、require("app.MyApp").new()
MyApp.new()執行後,執行的代碼是:
1
2
3
|
function
MyApp:ctor() MyApp.super.ctor(self) end |
1
2
3
4
5
6
7
8
|
function
cls. new (...) local
instance = cls.__create(...) --
copy fields from class to
native object for k,v
in pairs(cls) do instance[k]
= v end instance. class =
cls instance:ctor(...) return instance end |
3、require("app.MyApp").new():run()
這時候調用了
1
2
3
4
|
function
MyApp:run() CCFileUtils:sharedFileUtils():addSearchPath( "res/" ) self:enterScene( "MainScene" ) end |
對於MyApp.lua文件,如果我修改成下面的樣子,是不是你就理解了上面所做的事情:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
--
聲明類 MyApp
= class ( "MyApp" ,
cc.mvc.AppBase) ---
類構造方法 -- function
MyApp:ctor() MyApp.super.ctor(self) end ---
對應cpp版的 static create()方法 -- function
MyApp:create() local
myApp = MyApp. new () myApp:init() end ---
你自己的方法 --
@param self -- local
function launchMainScene(self) CCFileUtils:sharedFileUtils():addSearchPath( "res/" ) self:enterScene( "MainScene" ) end ---
init 方法 -- function
MyApp:init() --
add code here launchMainScene(self) end |
1
|
|
修改爲:
1
2
|
require( "app.MyApp" ) MyApp:create() |
三、MainScene.lua
enterScene("MainScene") 爲什麼可以切換場景?
我們看下MyApp的父類AppBase裏面:
1
2
3
4
5
6
|
function
AppBase:enterScene(sceneName, args, transitionType, time ,
more) local
scenePackageName = self. packageRoot .. ".scenes." ..
sceneName local
sceneClass = require(scenePackageName) local
scene = sceneClass. new (unpack(totable(args))) display.replaceScene(scene,
transitionType, time ,
more) end |
1
|
|
好了,其他的應該按照上面的思路基本都能知道爲什麼了。我就不一一列舉了。