Cocos2d-x-Lua演示樣例項目HelloLuajavascript
本篇博客介紹Cocos2d-x中Lua的實例項目,就是使用Cocos2d-x建立的初始項目執行所呈現的農場,這裏筆者取名爲HelloLua。本篇博客會具體在代碼中解析Cocos2d-x 3.1.1建立的Lua項目中實例,一些API的使用。java
注:本演示樣例項目在Mac系統下建立ios
首先咱們來建立一個Cocos2d-x Lua項目,在命令敲入類似如下命令c++
cocos new HelloLua -p com.wwj.hellolua -l lua -d ~/Cocos2dxProj數組
這樣咱們就在Cocos2dxProj文件夾下建立了一個名叫HelloLua的Lua項目app
進入咱們runtime-src文件夾下打開proj.ios_mac文件夾。雙擊使用Xcode打開咱們的項目:ide
使用Xcode對咱們的Lua項目進行編譯並執行,就會呈現一個如下效果的演示樣例遊戲:函數
看完效果圖以後,來看看咱們XCode裏面的項目文件結構,例如如下圖:工具
以上畫圈的有main.cpp、AppDelegate.h、AppDelegate.cpp、main.lua、hello2.luaoop
咱們如下一個一個來看:
首先來看main.cpp文件,這個文件就是咱們程序的入口文件,程序的執行時從這裏開始的
》》main.cpp
#include "AppDelegate.h" #include "cocos2d.h" USING_NS_CC; int main(int argc, char *argv[]) { AppDelegate app; return Application::getInstance()->run(); }
以上代碼咱們可以看到,在main.cpp裏,經過#include引入了兩個頭文件,一個是AppDelegate.h、一個是cocos2d.h。
定義了咱們程序的入口方法main,經過執行Application::getInstance()->run()方法來執行咱們的程序。
>>>AppDelegate.h
#ifndef __APP_DELEGATE_H__ #define __APP_DELEGATE_H__ #include "cocos2d.h" /** @brief The cocos2d Application. The reason for implement as private inheritance is to hide some interface call by Director. */ class AppDelegate : private cocos2d::Application { public: AppDelegate(); virtual ~AppDelegate(); /** @brief Implement Director and Scene init code here. @return true Initialize success, app continue. @return false Initialize failed, app terminate. */ virtual bool applicationDidFinishLaunching(); /** @brief The function be called when the application enter background @param the pointer of the application */ virtual void applicationDidEnterBackground(); /** @brief The function be called when the application enter foreground @param the pointer of the application */ virtual void applicationWillEnterForeground(); }; #endif // __APP_DELEGATE_H__
#include "AppDelegate.h" #include "CCLuaEngine.h" #include "SimpleAudioEngine.h" #include "cocos2d.h" using namespace CocosDenshion; USING_NS_CC; using namespace std; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { SimpleAudioEngine::end(); } bool AppDelegate::applicationDidFinishLaunching() { // initialize director 得到導演類實例 auto director = Director::getInstance(); // 得到OpenGL實例 auto glview = director->getOpenGLView(); if(!glview) { // 指定窗體大小 glview = GLView::createWithRect("HelloLua", Rect(0,0,900,640)); // 設置OpenGL視圖 director->setOpenGLView(glview); } // 設置分辨率大小爲480*320 glview->setDesignResolutionSize(480, 320, ResolutionPolicy::NO_BORDER); // turn on display FPS 打開幀頻,屏幕左下角哪一串數據 // 啓用FPS 顯示,當前 FPS 會在遊戲的左下角顯示。FPS也就是屏幕每秒重繪的次數。即每秒幀速率。在遊戲開發階段。可以方便地肯定遊戲執行是否流暢。 director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this // 設置繪製間隔 director->setAnimationInterval(1.0 / 60); // 得到Lua引擎實例 auto engine = LuaEngine::getInstance(); // 設置腳本引擎 ScriptEngineManager::getInstance()->setScriptEngine(engine); // 執行main.lua腳本 if (engine->executeScriptFile("src/main.lua")) { return false; } return true; } // This function will be called when the app is inactive. When comes a phone call,it's be invoked too // 當應用程序將要進入後臺時,會調用這種方法 void AppDelegate::applicationDidEnterBackground() { Director::getInstance()->stopAnimation(); SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } // this function will be called when the app is active again // 該方法與applicationDidEnterBackground() 成對出現。在應用程序回到前臺時被調用 void AppDelegate::applicationWillEnterForeground() { Director::getInstance()->startAnimation(); SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); }
>>>main.lua
這是Cocos2d-x 3.1.1所使用的代碼,代碼中的凝視已經很是具體了,筆者在這裏就很少說,但願你們能認真閱讀,跟筆者一塊兒儘快入門Lua在Cocos2dx中的使用。require "Cocos2d" require "Cocos2dConstants" -- cclog cclog = function(...) print(string.format(...)) end -- for CCLuaEngine traceback 輸出綁定執行函數發生錯誤的信息 function __G__TRACKBACK__(msg) cclog("----------------------------------------") cclog("LUA ERROR: " .. tostring(msg) .. "\n") cclog(debug.traceback()) cclog("----------------------------------------") return msg end local function main() collectgarbage("collect") -- avoid memory leak 這是腳本回收參數,避免內存泄漏 collectgarbage("setpause", 100) collectgarbage("setstepmul", 5000) -- 追加資源的搜索順序 cc.FileUtils:getInstance():addSearchResolutionsOrder("src"); cc.FileUtils:getInstance():addSearchResolutionsOrder("res"); local schedulerID = 0 --support debug 獲取目標平臺 local targetPlatform = cc.Application:getInstance():getTargetPlatform() if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or (cc.PLATFORM_OS_ANDROID == targetPlatform) or (cc.PLATFORM_OS_WINDOWS == targetPlatform) or (cc.PLATFORM_OS_MAC == targetPlatform) then cclog("result is ") --require('debugger')() end -- 類似c++的include,引入文件,會檢查是否反覆引入 require "hello2" -- 調用外部函數,在hello2.lua中 cclog("result is " .. myadd(1, 1)) --------------- -- 獲取可視區域 local visibleSize = cc.Director:getInstance():getVisibleSize() -- 可視原點座標 OpenGL座標系,左下角爲原點 local origin = cc.Director:getInstance():getVisibleOrigin() -- add the moving dog 加入移動的小松鼠 local function creatDog() -- 每一幀尺寸設置,local表示局部變量 local frameWidth = 105 local frameHeight = 95 -- create dog animate 載入動畫資源並建立精靈幀 -- 載入精靈動畫所在紋理 local textureDog = cc.Director:getInstance():getTextureCache():addImage("dog.png") -- 設置第一幀幀區域 local rect = cc.rect(0, 0, frameWidth, frameHeight) -- 建立第一幀精靈Frame local frame0 = cc.SpriteFrame:createWithTexture(textureDog, rect) -- 設置第二幀幀區域 rect = cc.rect(frameWidth, 0, frameWidth, frameHeight) -- c建立第二幀精靈Frame local frame1 = cc.SpriteFrame:createWithTexture(textureDog, rect) -- 基於使用第一幀Frame建立Sprite對象 local spriteDog = cc.Sprite:createWithSpriteFrame(frame0) spriteDog.isPaused = false spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3) --[[ local animFrames = CCArray:create() animFrames:addObject(frame0) animFrames:addObject(frame1) ]]-- -- 依據幀序列數組建立一個動畫animation。幀間隔時間delay等於0.5秒 local animation = cc.Animation:createWithSpriteFrames({frame0,frame1}, 0.5) -- 依據動畫animation建立動做實例 local animate = cc.Animate:create(animation); -- 松鼠精靈執行該動做 spriteDog:runAction(cc.RepeatForever:create(animate)) -- moving dog at every frame 用來更新松鼠的位置,後面會調用該函數 local function tick() if spriteDog.isPaused then return end local x, y = spriteDog:getPosition() if x > origin.x + visibleSize.width then x = origin.x else x = x + 1 end spriteDog:setPositionX(x) end -- 生成一個scheule,每幀執行tick函數 schedulerID = cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false) return spriteDog end -- create farm 建立地面的農場 local function createLayerFarm() -- 建立一個新的Lyaer用做農場管理 local layerFarm = cc.Layer:create() -- add in farm background 加入農場管理 local bg = cc.Sprite:create("farm.jpg") bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2) layerFarm:addChild(bg) -- add land sprite 加入地面磚塊 for i = 0, 3 do for j = 0, 1 do local spriteLand = cc.Sprite:create("land.png")、 -- 設定每一塊磚塊位置 spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2) layerFarm:addChild(spriteLand) end end -- add crop 加入莊稼,注意crop.png是多張圖的合成貼圖。因此僅僅取了裏面的部分貼圖 local frameCrop = cc.SpriteFrame:create("crop.png", cc.rect(0, 0, 105, 95)) for i = 0, 3 do for j = 0, 1 do local spriteCrop = cc.Sprite:createWithSpriteFrame(frameCrop); spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2) layerFarm:addChild(spriteCrop) end end -- add moving dog 調用上面的createDog()方面。建立一個移動的松鼠 local spriteDog = creatDog() layerFarm:addChild(spriteDog) -- handing touch events 手指觸摸事件處理 local touchBeginPoint = nil -- 手指點擊開始 local function onTouchBegan(touch, event) local location = touch:getLocation() cclog("onTouchBegan: %0.2f, %0.2f", location.x, location.y) touchBeginPoint = {x = location.x, y = location.y} -- 保存點擊位置 spriteDog.isPaused = true -- 將松鼠暫停移動 -- CCTOUCHBEGAN event must return true return true end -- 手指按住移動 local function onTouchMoved(touch, event) local location = touch:getLocation() cclog("onTouchMoved: %0.2f, %0.2f", location.x, location.y) if touchBeginPoint then -- 將整個農村層拖動。因爲以前已經將農場裏面所有對象加入layerFarm local cx, cy = layerFarm:getPosition() layerFarm:setPosition(cx + location.x - touchBeginPoint.x, cy + location.y - touchBeginPoint.y) touchBeginPoint = {x = location.x, y = location.y} end end -- 手指離開 local function onTouchEnded(touch, event) local location = touch:getLocation() cclog("onTouchEnded: %0.2f, %0.2f", location.x, location.y) touchBeginPoint = nil -- 點擊位置數據清空 spriteDog.isPaused = false -- 恢復松鼠移動 end -- 建立觸摸事件監聽器 local listener = cc.EventListenerTouchOneByOne:create() -- 註冊touch事件 listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN ) listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED ) listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED ) local eventDispatcher = layerFarm:getEventDispatcher() -- 加入場景圖優先級事件監聽 eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layerFarm) local function onNodeEvent(event) if "exit" == event then cc.Director:getInstance():getScheduler():unscheduleScriptEntry(schedulerID) end end layerFarm:registerScriptHandler(onNodeEvent) return layerFarm end -- create menu 建立界面菜單 local function createLayerMenu() -- 建立一個新的Layer管理所有菜單 local layerMenu = cc.Layer:create() local menuPopup, menuTools, effectID -- 點擊菜單回調函數 local function menuCallbackClosePopup() -- stop test sound effect 關閉音效 cc.SimpleAudioEngine:getInstance():stopEffect(effectID) menuPopup:setVisible(false) -- 隱藏菜單 end -- 點擊菜單回調函數 local function menuCallbackOpenPopup() -- loop test sound effect 打開營銷 local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav") effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath) menuPopup:setVisible(true) end -- add a popup menu 建立彈出的菜單面板 local menuPopupItem = cc.MenuItemImage:create("menu2.png", "menu2.png") menuPopupItem:setPosition(0, 0) menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup) menuPopup = cc.Menu:create(menuPopupItem) menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2) menuPopup:setVisible(false) layerMenu:addChild(menuPopup) -- add the left-bottom "tools" menu to invoke menuPopup -- 加入左下角的工具button。用來彈出菜單面板 local menuToolsItem = cc.MenuItemImage:create("menu1.png", "menu1.png") menuToolsItem:setPosition(0, 0) -- 註冊點擊回調地址 menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup) menuTools = cc.Menu:create(menuToolsItem) local itemWidth = menuToolsItem:getContentSize().width local itemHeight = menuToolsItem:getContentSize().height menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2) layerMenu:addChild(menuTools) return layerMenu end -- play background music, preload effect -- uncomment below for the BlackBerry version local bgMusicPath = nil if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) then bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("res/background.caf") else bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("res/background.mp3") end cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath, true) local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav") -- 預載入音效 cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath) -- run local sceneGame = cc.Scene:create() -- 建立場景 sceneGame:addChild(createLayerFarm()) -- 將農場層加入場景 sceneGame:addChild(createLayerMenu()) -- 將菜單界面層加入場景 -- 推斷是否有執行的場景 if cc.Director:getInstance():getRunningScene() then cc.Director:getInstance():replaceScene(sceneGame) -- 替換場景 else cc.Director:getInstance():runWithScene(sceneGame) end end --[[ xpcall( 調用函數, 錯誤捕獲函數 ); lua提供了xpcall來捕獲異常 xpcall接受兩個參數:調用函數、錯誤處理函數。 當發生錯誤時,Lua會在棧釋放曾經調用錯誤處理函數,所以可以使用debug庫收集錯誤相關信息。兩個常常使用的debug處理函數:debug.debug和debug.traceback 前者給出Lua的提示符,你可以本身動手察看發生錯誤時的狀況; 後者經過traceback建立不少其它的錯誤信息,也是控制檯解釋器用來構建錯誤信息的函數。 --]] local status, msg = xpcall(main, __G__TRACKBACK__) if not status then error(msg) end