Cocos2dx lua 啟動流程

2021-08-14 21:34:38 字數 2993 閱讀 1640

cocos2dx 版本 3.x,工具 vs2013 + babelua外掛程式

1.lua工具,babelua

2.cocos2dx 建立lua工程

windows 7下,配置好cocos2dx環境後,使用命令列建立專案。 

cocos new mylua1 -p com.your_company.mygame -l lua -d f:/lua/mylua1 

不會配置環境的同學可以檢視cocos2dx原始碼目錄下的readme.md

3.lua專案

#endif

return

true;}

與c++下的**相比,原先的view 初始化,opgl引數初始化等都沒有了,看樣子都移交給了lua模組了。 

但是vs2013在c++下是看不到lua的邏輯**的,這個時候就需要新建乙個vs2013的lua專案了,lua->new lua porject,可以看到新建vs的lua專案需要的設定: 

4.關於建立vs lua專案的引數

lua scripts folder:專案目錄 

lua exe path:載入lua指令碼exe程式 

working path:工作目錄 

command line:傳給lua指令碼的編譯引數 

lua project name:lua vs下的名稱

從這裡可以看到,我們首先需要個exe程式,很顯然,cocos2dx c++模組就是乙個框架exe,然後載入lua來執行不同的業務。直接執行mylua1專案,會產生exe程式。 

之後設定新建lua專案的引數: 

5.嘗試修改lua指令碼並執行 

main.lua

local

function

main

()end

local status, msg = xpcall(main, __g__trackback__)

ifnot status then

print(msg)

end

function

() math.randomseed(os.time())

end

:create()的時候呼叫

:ctor(configs)

--注意下面的幾個成員變數

self.configs_ =

for k, v in pairs(configs or {}) do

self.configs_[k] = v

endif type(self.configs_.viewsroot) ~= "table"

then

self.configs_.viewsroot =

endif type(self.configs_.modelsroot) ~= "table"

then

self.configs_.modelsroot =

endifdebug > 1

then

endifcc_show_fps

then

cc.director

:getinstance():setdisplaystats(true)

end-- event

self:oncreate()

end

function

(initscenename)

initscenename = initscenename or self.configs_.defaultscenename --指定initscenenname

self:enterscene(initscenename) --呼叫enterscene

end

enterscene()進入具體的場景邏輯了,相當於c++下呼叫director::runwithscene()的作用

function

local view = self:createview(scenename)

view:showwithscene(transition, time, more)

return view

end

function mainscene

:oncreate()

-- add background image

display.newsprite("helloworld.png")

:move(display.center)

:addto(self)

-- add helloworld label,修改下label內容

cc.label

:createwithsystemfont("hello golds", "arial", 40)

:move(display.cx, display.cy + 200)

:addto(self)

end

執行 lua->run without debugging 

結果發現顯示的內容沒有更改,怎麼回事呢?

發現建立lua專案的時候 設定的working path問題,vs2013 預設將原來的指令碼**複製到了 working path下了,所以改下設定; 

選中lua專案,右擊->properties,即修改working path: 

重新執行就ok了: 

Cocos2d x Lua基本操作

1.lua庫引用 目錄新增 lua lua luajit include lib新增 lua51.lib 2.開啟lua庫 示例 lua state pl lua open luaopen base pl luaopen math pl luaopen string pl 3.讀取lua值 示例 1...

cocos2dx lua優化總結

渲染效率 紋理格式 執行效率 記憶體 包大小 cpp view plain copy 安卓啟用4444紋理 iftargetplatform cc.platform os android then cc.texture2d setdefaultalphapixelformat cc.texture2...

cocos2dx lua 技巧收藏

一,語法糖 冒號呼叫 在cocos2d lua 裡面經常看到到 node move addto 這些方法 只需要在 函式 return self 即可 二 型別判斷 因為lua 是沒有指定型別的 所以經常用到 type 變數名 經常看到有人 這樣用 type 變數 string 每次都要寫 很有可能...