Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理)
本篇博客介绍Cocos2d-x的动作管理样例,这个样例展示了Cocos2d-x的几个动作:
MoveTo——移动动作,移动到某一个点
MoveBy——移动动作,与MoveTo是相似的,仅仅是MoveBy能够移动到某一个点然后按原路返回,提供reverse方法。RotateTo——旋转动作,把某一精灵旋转到某一角度
RotateBy——旋转动作,把某一精灵旋转某个角度,它有一个方法reverse,它让对象按原路径旋转回ScaleTo——缩放动作,把某一精灵(Sprite)放大或缩小到某一比例Scaleby——缩放动作,把某一精灵(Sprite)放大或缩小多少比例,它有一个方法reverse,它让对象按原路径旋转回这个样例涉及到的知识点有:
- 创建动作序列,比如: cc.Sequence:create(cc.DelayTime:create(1.4),cc.CallFunc:create(removeThis)
- 执行动作序列,比如:ret:runAction( cc.Sequence:create(cc.DelayTime:create(1.4),cc.CallFunc:create(removeThis)))
local kTagNode = 0 -- 结点标识
local kTagGrossini = 1 --
local kTagSequence = 2 --
-- 获取和这个 director 关联的调度器
local scheduler = cc.Director:getInstance():getScheduler()
--------------------------------------------------------------------
--
-- Test1
--
--------------------------------------------------------------------local function CrashTest()-- 创建測试层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.-- 旋转一个节点,1.5秒,旋转90度child:runAction(cc.RotateBy:create(1.5, 90))-- 执行动作序列,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)-- 加入一个子节点到容器中,有Z轴顺序和一个标记。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-- 执行动作序列grossini: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))-- 创建移动动作,持续时间1秒,移动到(150,0)的位置local action = cc.MoveBy:create(1, cc.p(150,0))local pDirector = cc.Director:getInstance()-- 通过获取director关联的ActionManager并为目标加入动作-- 为一个目标加入动作。 假设目标已经存在,动作将被加在已经存在的目标上。-- 假设目标不存在,将会创建这个目标的新对象,这个动作将被加入在这个新创建出来的对象上 当目标动作被暂停,动作队列的顺序也不会乱。pDirector:getActionManager():addAction(action, grossini, true)schedulerEntry = scheduler:scheduleScriptFunc(unpause, 3.0, false)-- 退出elseif event == "exit" thenif schedulerEntry ~= nil thenscheduler:unscheduleScriptEntry(schedulerEntry)endendend-- 注冊响应事件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))-- 创建移动动作,持续2秒,到(200,0)的位置local pMove = cc.MoveBy:create(2, cc.p(200, 0))-- 停止动作local function stopAction()-- 依据Tag来获取子节点local pSprite = ret:getChildByTag(kTagGrossini)pSprite:stopActionByTag(kTagSequence)end-- 创建一个回调函数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
-- 恢复測试
--------------------------------------------------------------------
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)-- 执行缩放的动作pGrossini:runAction(cc.ScaleBy:create(2, 2))local pDirector = cc.Director:getInstance()-- 暂停目标pDirector:getActionManager():pauseTarget(pGrossini)-- 执行旋转动作,旋转360度,持续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-- 设置深度測试cc.Director:getInstance():setDepthTest(true)-- 创建场景local scene = cc.Scene:create()-- 初始化方法表Helper.createFunctionTable = {CrashTest,LogicTest,PauseTest,RemoveTest,ResumeTest}-- 加入层scene:addChild(CrashTest())scene:addChild(CreateBackMenuItem())return scene
end
在样例中用到一些定义好的资源路径,还有相关帮助类,童鞋们能够到能够到对应文件夹下进行查找:
require "Cocos2d"CC_CONTENT_SCALE_FACTOR = function()-- 获取surface的大小,单位为像素return cc.Director:getInstance():getContentScaleFactor()
end--把以像素为单位的矩形转换为以点为单位的矩形
CC_POINT_PIXELS_TO_POINTS = function(pixels)return cc.p(pixels.x/CC_CONTENT_SCALE_FACTOR(), pixels.y/CC_CONTENT_SCALE_FACTOR())
end-- 把以点为单位的矩形转换为以像素为单位的矩形
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 把表转换为枚举类型
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 返回菜单回调
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()-- 创建一个标签local label = cc.Label:createWithTTF("MainMenu", s_arialPath, 20)-- 设置器锚点label:setAnchorPoint(cc.p(0.5, 0.5))-- 设置菜单项标签local MenuItem = cc.MenuItemLabel:create(label)MenuItem:registerScriptTapHandler(MainMenuCallback)-- 获得屏幕大小local s = cc.Director:getInstance():getWinSize()-- 创建菜单local Menu = cc.Menu:create()-- 加入菜单项Menu:addChild(MenuItem)-- 设置菜单位置Menu:setPosition(0, 0)-- 设置菜单项位置,大致在右下角的位置MenuItem:setPosition(s.width - 50, 25)return Menu
end-- 帮助类
Helper = {index = 1, -- 索引createFunctioinTable = nil, -- 存储方法的表currentLayer = nil, -- 当前层titleLabel = nil, -- 标题subtitleLabel = nil -- 子标题
}-- 下个动作
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-- 假设使用物理效果if Helper.usePhysics then-- 创建一个带物理效果的场景scene = cc.Scene:createWithPhysics()elsescene = cc.Scene:create()endHelper.currentLayer = Helper.createFunctionTable[Helper.index]()-- 加入当前层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-- 创建測试层
function createTestLayer(title, subtitle)-- 创建一个层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"
Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理)相关推荐
- Cocos2d-x 3.2 Lua演示样例FontTest(字体測试)
Cocos2d-x 3.2 Lua演示样例FontTest(字体測试) 本篇博客介绍Cocos2d-x 3.2中Lua測试项目中的FontTest样例,主要使用了字体文件来创建我们想要的字体样式: 第 ...
- JDBC连接MySQL数据库及演示样例
JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识 JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...
- Oracle简单脚本演示样例
Oracle简单脚本演示样例 1.添加表 --改动日期:2014.09.21 --改动人:易小群 --改动内容:新增採购支付情况表 DECLARE VC_STR VARCHAR2( ...
- EEPlat vs saleforce 配置 Knowledge Article 演示样例
==================================================================================================== ...
- 模式识别 - 处理多演示样例学习(MIL)特征(matlab)
处理多演示样例学习(MIL)特征(matlab) 本文地址: http://blog.csdn.net/caroline_wendy/article/details/27206325 多演示样例学习( ...
- 百度地图 Android SDK - 检索功能使用的简单演示样例
百度地图 SDK 不仅为广大开发人员提供了炫酷的地图展示效果.丰富的覆盖物图层,更为广大开发人员提供了多种 LBS 检索的能力. 通过这些接口,开发人员能够轻松的訪问百度的 LBS 数据,丰富自己的移 ...
- RPM安装包-Spec文件參数具体解释与演示样例分析
spec文件是整个RPM包建立过程的中心,它的作用就如同编译程序时的Makefile文件. 1.Spec文件參数 spec文件包括建立一个RPM包必需的信息,包括哪些文件是包的一部分以及它们安装在哪个 ...
- Androidclient与服务端交互之登陆演示样例
今天了解了一下androidclient与服务端是如何交互的,发现事实上跟web有点类似吧,然后网上找了大神的登陆演示样例.是基于IntentService的 1.后台使用简单的servlet,支持G ...
- JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例
什么是 JBoss MSC JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MS ...
最新文章
- 求1到30的阶乘和(Java)
- SSD固态存储大观(二)
- mac 使用远程连接
- spark broadcast的TorrentBroadcast实现
- GitHub 日收 7000 星,Windows 计算器项目开源即爆红!
- 21天Jmeter打卡day16参数传递token传递,接口关联
- 关于Windows7下 IIS 7 对ASP.NET 的配置的一点感想
- linux如何使用eth0网卡,Linux运维学习之如何取出网卡eth0的ip地址?
- oracle月份相减函数,Oracle 日期函数的加减
- IDEA项目打包几种方式
- 泛函分析 06.04 线性算子的谱理论 - 紧线性算子的谱
- Visual studio插件:Visual Svn 替代工具--AnkhSvn
- System Repair Engineer (SREng) 2.5 常用操作
- Android应用程序启动时出现白色背景问题
- 综合微软、AMiner两大学术图谱,清华大学唐杰博士如何将Open Academic Graph亿级数据精准匹配
- 黑桃spade,红桃heart,方片diamond,梅花club
- emacs java_Emacs配置与使用
- 51Nod1740 蜂巢迷宫
- 快鲸科技邀您一起合作,共同发展
- php网站 域名授权 怎么破,使用php进行域名授权代码 - 小俊学习网