日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理)

發(fā)布時間:2023/12/31 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Cocos2d-x 3.1.1 Lua演示樣例 ActionManagerTest(動作管理)

本篇博客介紹Cocos2d-x的動作管理樣例,這個樣例展示了Cocos2d-x的幾個動作:MoveTo——移動動作,移動到某一個點MoveBy——移動動作,與MoveTo是相似的,僅僅是MoveBy能夠移動到某一個點然后按原路返回,提供reverse方法。RotateTo——旋轉(zhuǎn)動作,把某一精靈旋轉(zhuǎn)到某一角度
RotateBy——旋轉(zhuǎn)動作,把某一精靈旋轉(zhuǎn)某個角度,它有一個方法reverse,它讓對象按原路徑旋轉(zhuǎn)回ScaleTo——縮放動作,把某一精靈(Sprite)放大或縮小到某一比例Scaleby——縮放動作,把某一精靈(Sprite)放大或縮小多少比例,它有一個方法reverse,它讓對象按原路徑旋轉(zhuǎn)回

這個樣例涉及到的知識點有:
  • 創(chuàng)建動作序列,比如:?cc.Sequence:create(cc.DelayTime:create(1.4),cc.CallFunc:create(removeThis)
  • 執(zhí)行動作序列,比如:ret:runAction( cc.Sequence:create(cc.DelayTime:create(1.4),cc.CallFunc:create(removeThis)))

我們先來看一下這個樣例的效果,特別制作動態(tài)圖給大家展示:






詳細代碼實現(xiàn),詳細API的用法,希望各位能認真看,假設(shè)對當(dāng)中所傳參數(shù)不清楚和不理解的,能夠請教百度老師或者到官網(wǎng)參考詳細API,這是學(xué)習(xí)方法。》》》ActionManagerTest.lua
local kTagNode = 0 -- 結(jié)點標(biāo)識 local kTagGrossini = 1 -- local kTagSequence = 2 -- -- 獲取和這個 director 關(guān)聯(lián)的調(diào)度器 local scheduler = cc.Director:getInstance():getScheduler() -------------------------------------------------------------------- -- -- Test1 -- --------------------------------------------------------------------local function CrashTest()-- 創(chuàng)建測試層local ret = createTestLayer("Test 1. Should not crash")-- 精靈,s_pPathGrossini為圖片路徑local child = cc.Sprite:create(s_pPathGrossini)-- 顯示到x=200,y=200的位置child:setPosition( 200,200 )ret:addChild(child, 1)--Sum of all action's duration is 1.5 second.-- 旋轉(zhuǎn)一個節(jié)點,1.5秒,旋轉(zhuǎn)90度child:runAction(cc.RotateBy:create(1.5, 90))-- 執(zhí)行動作序列,1.4秒延遲,淡出child:runAction(cc.Sequence:create(cc.DelayTime:create(1.4),cc.FadeOut:create(1.1)))local function removeThis()-- 溢出孩子ret:getParent():removeChild(ret, true)Helper.nextAction()end--After 1.5 second, self will be removed.-- 1.5秒之后,自身會被移除ret:runAction( cc.Sequence:create(cc.DelayTime:create(1.4),cc.CallFunc:create(removeThis)))return ret end-------------------------------------------------------------------- -- -- LogicTest -- 邏輯測試 -------------------------------------------------------------------- local function LogicTest()local ret = createTestLayer("Logic test")-- 精靈,s_pPathGrossini為圖片路徑local grossini = cc.Sprite:create(s_pPathGrossini)-- 加入一個子節(jié)點到容器中,有Z軸順序和一個標(biāo)記。ret:addChild(grossini, 0, 2)grossini:setPosition(200,200)local function bugMe(node)-- 停止全部動作node:stopAllActions() --After this stop next action not working, if remove this stop everything is workingnode:runAction(cc.ScaleTo:create(2, 2))end-- 執(zhí)行動作序列g(shù)rossini:runAction( cc.Sequence:create(cc.MoveBy:create(1, cc.p(150,0)) ,cc.CallFunc:create(bugMe)))return ret end-------------------------------------------------------------------- -- -- PauseTest -- 暫停測試 --------------------------------------------------------------------local function PauseTest()local ret = createTestLayer("Pause Test")local schedulerEntry = nillocal function unpause(dt)scheduler:unscheduleScriptEntry(schedulerEntry)schedulerEntry = nillocal node = ret:getChildByTag( kTagGrossini )local pDirector = cc.Director:getInstance()pDirector:getActionManager():resumeTarget(node)endlocal function onNodeEvent(event)-- 進入時if event == "enter" thenlocal s = cc.Director:getInstance():getWinSize()local l = cc.Label:createWithTTF("After 3 seconds grossini should move", "fonts/Thonburi.ttf", 16)ret:addChild(l)l:setAnchorPoint(cc.p(0.5, 0.5))l:setPosition( cc.p(s.width / 2, 245) )local grossini = cc.Sprite:create(s_pPathGrossini)ret:addChild(grossini, 0, kTagGrossini)grossini:setPosition(cc.p(200,200))-- 創(chuàng)建移動動作,持續(xù)時間1秒,移動到(150,0)的位置local action = cc.MoveBy:create(1, cc.p(150,0))local pDirector = cc.Director:getInstance()-- 通過獲取director關(guān)聯(lián)的ActionManager并為目標(biāo)加入動作-- 為一個目標(biāo)加入動作。 假設(shè)目標(biāo)已經(jīng)存在,動作將被加在已經(jīng)存在的目標(biāo)上。-- 假設(shè)目標(biāo)不存在,將會創(chuàng)建這個目標(biāo)的新對象,這個動作將被加入在這個新創(chuàng)建出來的對象上 當(dāng)目標(biāo)動作被暫停,動作隊列的順序也不會亂。pDirector:getActionManager():addAction(action, grossini, true)schedulerEntry = scheduler:scheduleScriptFunc(unpause, 3.0, false)-- 退出elseif event == "exit" thenif schedulerEntry ~= nil thenscheduler:unscheduleScriptEntry(schedulerEntry)endendend-- 注冊響應(yīng)事件ret:registerScriptHandler(onNodeEvent)return ret end-------------------------------------------------------------------- -- -- RemoveTest -- -------------------------------------------------------------------- local function RemoveTest()local ret = createTestLayer("Remove Test")local l = cc.Label:createWithTTF("Should not crash", "fonts/Thonburi.ttf", 16)-- 獲得屏幕大小local s = cc.Director:getInstance():getWinSize()ret:addChild(l)l:setAnchorPoint(cc.p(0.5, 0.5))l:setPosition( cc.p(s.width / 2, 245))-- 創(chuàng)建移動動作,持續(xù)2秒,到(200,0)的位置local pMove = cc.MoveBy:create(2, cc.p(200, 0))-- 停止動作local function stopAction()-- 依據(jù)Tag來獲取子節(jié)點local pSprite = ret:getChildByTag(kTagGrossini)pSprite:stopActionByTag(kTagSequence)end-- 創(chuàng)建一個回調(diào)函數(shù)local callfunc = cc.CallFunc:create(stopAction)local pSequence = cc.Sequence:create(pMove,callfunc)pSequence:setTag(kTagSequence)local pChild = cc.Sprite:create(s_pPathGrossini)pChild:setPosition( 200, 200 )ret:addChild(pChild, 1, kTagGrossini)pChild:runAction(pSequence)return ret end-------------------------------------------------------------------- -- -- ResumeTest -- 恢復(fù)測試 -------------------------------------------------------------------- local function ResumeTest()local ret = createTestLayer("Resume Test")local schedulerEntry = nillocal function resumeGrossini(time)scheduler:unscheduleScriptEntry(schedulerEntry)schedulerEntry = nillocal pGrossini = ret:getChildByTag(kTagGrossini)local pDirector = cc.Director:getInstance()pDirector:getActionManager():resumeTarget(pGrossini)endlocal function onNodeEvent(event)if event == "enter" thenlocal l = cc.Label:createWithTTF("Grossini only rotate/scale in 3 seconds", "fonts/Thonburi.ttf", 16)ret:addChild(l)local s = cc.Director:getInstance():getWinSize()l:setAnchorPoint(cc.p(0.5, 0.5))l:setPosition( s.width / 2, 245)local pGrossini = cc.Sprite:create(s_pPathGrossini)ret:addChild(pGrossini, 0, kTagGrossini)pGrossini:setPosition(200,200)-- 執(zhí)行縮放的動作pGrossini:runAction(cc.ScaleBy:create(2, 2))local pDirector = cc.Director:getInstance()-- 暫停目標(biāo)pDirector:getActionManager():pauseTarget(pGrossini)-- 執(zhí)行旋轉(zhuǎn)動作,旋轉(zhuǎn)360度,持續(xù)2秒pGrossini:runAction(cc.RotateBy:create(2, 360))schedulerEntry = scheduler:scheduleScriptFunc(resumeGrossini, 3.0, false)elseif event == "exit" thenif schedulerEntry ~= nil thenscheduler:unscheduleScriptEntry(schedulerEntry)endendendret:registerScriptHandler(onNodeEvent)return ret endfunction ActionManagerTestMain()cclog("ActionManagerTestMain")Helper.index = 1 -- 初始索引為1-- 設(shè)置深度測試cc.Director:getInstance():setDepthTest(true)-- 創(chuàng)建場景l(fā)ocal scene = cc.Scene:create()-- 初始化方法表Helper.createFunctionTable = {CrashTest,LogicTest,PauseTest,RemoveTest,ResumeTest}-- 加入層scene:addChild(CrashTest())scene:addChild(CreateBackMenuItem())return scene end
在樣例中用到一些定義好的資源路徑,還有相關(guān)幫助類,童鞋們能夠到能夠到對應(yīng)文件夾下進行查找:help.lua(幫助類,封裝定義了相關(guān)方法,創(chuàng)建測試層、切換場景等)require "Cocos2d"CC_CONTENT_SCALE_FACTOR = function()-- 獲取surface的大小,單位為像素return cc.Director:getInstance():getContentScaleFactor() end--把以像素為單位的矩形轉(zhuǎn)換為以點為單位的矩形 CC_POINT_PIXELS_TO_POINTS = function(pixels)return cc.p(pixels.x/CC_CONTENT_SCALE_FACTOR(), pixels.y/CC_CONTENT_SCALE_FACTOR()) end-- 把以點為單位的矩形轉(zhuǎn)換為以像素為單位的矩形 CC_POINT_POINTS_TO_PIXELS = function(points)return cc.p(points.x*CC_CONTENT_SCALE_FACTOR(), points.y*CC_CONTENT_SCALE_FACTOR()) end-- cclog 打印日志 cclog = function(...)print(string.format(...)) end-- change table to enum type 把表轉(zhuǎn)換為枚舉類型 function CreateEnumTable(tbl, index)local enumTable = {}local enumIndex = index or -1for i, v in ipairs(tbl) doenumTable[v] = enumIndex + iendreturn enumTable end-- back menu callback 返回菜單回調(diào) local function MainMenuCallback()local scene = cc.Scene:create()scene:addChild(CreateTestMenu())-- 切換場景cc.Director:getInstance():replaceScene(scene) end-- add the menu item for back to main menu -- 為返回主菜單加入菜單項 function CreateBackMenuItem()-- 創(chuàng)建一個標(biāo)簽local label = cc.Label:createWithTTF("MainMenu", s_arialPath, 20)-- 設(shè)置器錨點label:setAnchorPoint(cc.p(0.5, 0.5))-- 設(shè)置菜單項標(biāo)簽local MenuItem = cc.MenuItemLabel:create(label)MenuItem:registerScriptTapHandler(MainMenuCallback)-- 獲得屏幕大小local s = cc.Director:getInstance():getWinSize()-- 創(chuàng)建菜單local Menu = cc.Menu:create()-- 加入菜單項Menu:addChild(MenuItem)-- 設(shè)置菜單位置Menu:setPosition(0, 0)-- 設(shè)置菜單項位置,大致在右下角的位置MenuItem:setPosition(s.width - 50, 25)return Menu end-- 幫助類 Helper = {index = 1, -- 索引createFunctioinTable = nil, -- 存儲方法的表currentLayer = nil, -- 當(dāng)前層titleLabel = nil, -- 標(biāo)題subtitleLabel = nil -- 子標(biāo)題 }-- 下個動作 function Helper.nextAction()Helper.index = Helper.index + 1 -- 索引加1if Helper.index > table.getn(Helper.createFunctionTable) thenHelper.index = 1 endreturn Helper.newScene() end-- 回退動作 function Helper.backAction()Helper.index = Helper.index - 1if Helper.index == 0 thenHelper.index = table.getn(Helper.createFunctionTable)endreturn Helper.newScene() end-- 又一次開始動作 function Helper.restartAction()return Helper.newScene() end-- 切換新的場景 function Helper.newScene()local scene-- 假設(shè)使用物理效果if Helper.usePhysics then-- 創(chuàng)建一個帶物理效果的場景scene = cc.Scene:createWithPhysics()elsescene = cc.Scene:create()endHelper.currentLayer = Helper.createFunctionTable[Helper.index]()-- 加入當(dāng)前層scene:addChild(Helper.currentLayer)-- 加入返回菜單scene:addChild(CreateBackMenuItem())-- 切換場景cc.Director:getInstance():replaceScene(scene) end-- 初始化層 function Helper.initWithLayer(layer)Helper.currentLayer = layer-- 獲取屏幕大小local size = cc.Director:getInstance():getWinSize()Helper.titleLabel = cc.Label:createWithTTF("", s_arialPath, 28)Helper.titleLabel:setAnchorPoint(cc.p(0.5, 0.5))layer:addChild(Helper.titleLabel, 1)Helper.titleLabel:setPosition(size.width / 2, size.height - 50)Helper.subtitleLabel = cc.Label:createWithTTF("", s_thonburiPath, 16)Helper.subtitleLabel:setAnchorPoint(cc.p(0.5, 0.5))layer:addChild(Helper.subtitleLabel, 1)Helper.subtitleLabel:setPosition(size.width / 2, size.height - 80)-- menu菜單local item1 = cc.MenuItemImage:create(s_pPathB1, s_pPathB2)local item2 = cc.MenuItemImage:create(s_pPathR1, s_pPathR2)local item3 = cc.MenuItemImage:create(s_pPathF1, s_pPathF2)item1:registerScriptTapHandler(Helper.backAction)item2:registerScriptTapHandler(Helper.restartAction)item3:registerScriptTapHandler(Helper.nextAction)local menu = cc.Menu:create()menu:addChild(item1)menu:addChild(item2)menu:addChild(item3)menu:setPosition(cc.p(0, 0))-- 擺放三個菜單項的位置item1:setPosition(cc.p(size.width / 2 - item2:getContentSize().width * 2, item2:getContentSize().height / 2))item2:setPosition(cc.p(size.width / 2, item2:getContentSize().height / 2))item3:setPosition(cc.p(size.width / 2 + item2:getContentSize().width * 2, item2:getContentSize().height / 2))layer:addChild(menu, 1)local background = cc.Layer:create()layer:addChild(background, -10) end-- 創(chuàng)建測試層 function createTestLayer(title, subtitle)-- 創(chuàng)建一個層local layer = cc.Layer:create()Helper.initWithLayer(layer)local titleStr = title == nil and "No title" or titlelocal subTitleStr = subtitle == nil and "" or subtitleHelper.titleLabel:setString(titleStr)Helper.subtitleLabel:setString(subTitleStr)return layer end
testResource.lua定義了全部的資源路徑,方便管理和獲取s_pPathGrossini = "Images/grossini.png" s_pPathSister1 = "Images/grossinis_sister1.png" s_pPathSister2 = "Images/grossinis_sister2.png" s_pPathB1 = "Images/b1.png" s_pPathB2 = "Images/b2.png" s_pPathR1 = "Images/r1.png" s_pPathR2 = "Images/r2.png" s_pPathF1 = "Images/f1.png" s_pPathF2 = "Images/f2.png" s_pPathBlock = "Images/blocks.png" s_back = "Images/background.png" s_back1 = "Images/background1.png" s_back2 = "Images/background2.png" s_back3 = "Images/background3.png" s_stars1 = "Images/stars.png" s_stars2 = "Images/stars2.png" s_fire = "Images/fire.png" s_snow = "Images/snow.png" s_streak = "Images/streak.png" s_PlayNormal = "Images/btn-play-normal.png" s_PlaySelect = "Images/btn-play-selected.png" s_AboutNormal = "Images/btn-about-normal.png" s_AboutSelect = "Images/btn-about-selected.png" s_HighNormal = "Images/btn-highscores-normal.png" s_HighSelect = "Images/btn-highscores-selected.png" s_Ball = "Images/ball.png" s_Paddle = "Images/paddle.png" s_pPathClose = "Images/close.png" s_MenuItem = "Images/menuitemsprite.png" s_SendScore = "Images/SendScoreButton.png" s_PressSendScore = "Images/SendScoreButtonPressed.png" s_Power = "Images/powered.png" s_AtlasTest = "Images/atlastest.png"-- tilemaps resource s_TilesPng = "TileMaps/tiles.png" s_LevelMapTga = "TileMaps/levelmap.tga"-- spine test resource s_pPathSpineBoyJson = "spine/spineboy.json" s_pPathSpineBoyAtlas = "spine/spineboy.atlas"-- fonts resource s_markerFeltFontPath = "fonts/Marker Felt.ttf" s_arialPath = "fonts/arial.ttf" s_thonburiPath = "fonts/Thonburi.ttf" s_tahomaPath = "fonts/tahoma.ttf"




? ? ?

總結(jié)

以上是生活随笔為你收集整理的Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。