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)))
我们先来看一下这个例子的效果,特别制作动态图给大家展示:
![](https://i-blog.csdnimg.cn/blog_migrate/bdbe0ef4dc716467fe1c07a29cb6e1df.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/f6dd80c6bb46a0e68acd815811547e48.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e9ddb40123275d630f772ee2971c07ae.gif)
具体代码实现,具体API的使用方法,希望各位能认真看,如果对其中所传参数不清楚和不理解的,可以请教百度老师或者到官网参考具体API,这是学习方法。
》》》ActionManagerTest.lua
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 retend------------------------------------------------------------------------ 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