Cocos2d-x-Lua演示樣例項目HelloLua
本篇博客介紹Cocos2d-x中Lua的實例項目,就是使用Cocos2d-x創建的初始項目執行所呈現的農場,這里筆者取名為HelloLua。本篇博客會具體在代碼中解析Cocos2d-x 3.1.1創建的Lua項目中實例,一些API的使用。
注:本演示樣例項目在Mac系統下創建
首先我們來創建一個Cocos2d-x Lua項目,在命令敲入相似以下命令
cocos new HelloLua -p com.wwj.hellolua -l lua -d ~/Cocos2dxProj
這樣我們就在Cocos2dxProj文件夾下創建了一個名叫HelloLua的Lua項目
進入我們runtime-src文件夾下打開proj.ios_mac文件夾。雙擊使用Xcode打開我們的項目:
使用Xcode對我們的Lua項目進行編譯並執行,就會呈現一個以下效果的演示樣例游戲:
看完效果圖之后,來看看我們XCode里面的項目文件結構,例如以下圖:
以上畫圈的有main.cpp、AppDelegate.h、AppDelegate.cpp、main.lua、hello2.lua
我們以下一個一個來看:
首先來看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__
>>>AppDelegate.cpp
#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(); }
我們在AppDelegate類其中能夠找到執行我們Lua腳本的方法,以下來看一下main.lua這個文件,我們屏幕顯示的邏輯實現全部在這個文件里能夠看到:
>>>main.lua
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
這是Cocos2d-x 3.1.1所使用的代碼,代碼中的凝視已經非常具體了,筆者在這里就不多說,希望大家能認真閱讀,跟筆者一起盡快入門Lua在Cocos2dx中的使用。