本篇博客介绍Cocos2d-x中的动作,Cocos2d-x为我们提供了丰富的动作接口,下面笔者就详细介绍一下: 本系列博客介绍的是lua的实现,想看C++实现请移步到Cocos2d-x为我们提供的例子: Action类继承关系图,各位可以到http://cn.cocos2d-x.org/doc/cocos2d-x-3.0/db/d61/classcocos2d_1_1_action.html进行参考: 本篇博客的例子并没有涉及所有的动作,其他动作,以后的例子可能会涉及到,到时再进行说明。 我们先看一下效果,笔者只做了第一个动作的gif图,剩下的,读者可以自己运行lua-tests进行查看
本例子动作总结: MoveBy/MoveTo: 创建一个移动的动作 EaseIn/EaseOut: 动作由慢变快/动作由快变慢 EaseInOut:动作由慢变快再由快变慢 EaseExponentialIn:动作由慢变极快 EaseExponentialOut:动作由极快变慢 EaseExponentialInOut:动作由慢至极快再由极快边慢 EaseSineIn:动作由快变慢 EaseSineOut:动作由慢变快 EaseSineInOut:精灵由慢至快再由快至慢 EaseElasticIn:让目标动作赋予弹性 ,且以目标动作起点位子赋予弹性
EaseElasticOut:让目标动作赋予弹性 ,且以目标动作终点位子赋予弹性
EaseElasticInOut:让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性
EaseBounceIn:让目标动作缓慢开始
EaseBounceOut:让目标动作赋予反弹力,且以目标动作结束位子开始反弹 EaseBounceInOut:让目标动作赋予反弹力,且以目标动作起始与结束位子开始反弹
EaseBackIn:让目标动作赋予回力 , 且以目标动作起点位置作为回力点
EaseBackOut:让目标动作赋予回力 , 且以目标动作终点位置作为回力点
EaseBackInOut:让目标动作赋予回力 , 且以目标动作起点和终点位置作为回力点 JumpBy/JumpTo:跳的动作 RotateBy/RotateTo:旋转的动作
Spawn:让多个动作同时执行
Speed:让目标运行速度加倍
实现代码如下:
local kTagAction1 = 1local kTagAction2 = 2
local kTagSlider = 1
-- 获取屏幕大小
local s = cc.Director:getInstance():getWinSize()
-- 获取与Directorg关联的调度器
local scheduler = cc.Director:getInstance():getScheduler()
-- 创建简单的移动,持续3秒
local function createSimpleMoveBy()
return cc.MoveBy:create(3, cc.p(s.width - 130, 0))
end
-- 创建简单的延迟,0.25秒
local function createSimpleDelayTime()
return cc.DelayTime:create(0.25)
end
-- 设置两个精灵的位置
local function positionForTwo()
grossini:setPosition(cc.p(60, s.height * 1 / 5))
tamara:setPosition(cc.p(60, s.height * 4 / 5))
kathia:setVisible(false)
end
-- 获取基础层
local function getBaseLayer()
local layer = cc.Layer:create()
-- 三个丑陋的精灵
grossini = cc.Sprite:create(s_pPathGrossini)
tamara = cc.Sprite:create(s_pPathSister1)
kathia = cc.Sprite:create(s_pPathSister2)
-- 添加三个精灵到层当中
layer:addChild(grossini, 3)
layer:addChild(kathia, 2)
layer:addChild(tamara, 1)
-- 设置三个精灵的位置
grossini:setPosition(cc.p(60, s.height * 1 / 5))
kathia:setPosition(cc.p(60, s.height * 2.5 / 5))
tamara:setPosition(cc.p(60, s.height * 4 / 5))
-- 初始化层
Helper.initWithLayer(layer)
return layer
end
-----------------------------------
-- SpriteEase
-----------------------------------
local SpriteEase_entry = nil
-- 测试停止动作
local function testStopAction(dt)
-- 取消调度脚本
scheduler:unscheduleScriptEntry(SpriteEase_entry)
tamara:stopActionByTag(1)
kathia:stopActionByTag(1)
grossini:stopActionByTag(1)
end
-- 进入或退出
local function SpriteEase_onEnterOrExit(tag)
-- 如果是进入
if tag == "enter" then
-- 执行脚本方法
SpriteEase_entry = scheduler:scheduleScriptFunc(testStopAction, 6.25, false)
elseif tag == "exit" then
-- 注销循环
scheduler:unscheduleScriptEntry(SpriteEase_entry)
end
end
--
local function SpriteEase()
local layer = getBaseLayer()
local move = createSimpleMoveBy()
local move_back = move:reverse() -- 原地返回
-- 动作由慢变快
local move_ease_in = cc.EaseIn:create(createSimpleMoveBy(), 2.5)
-- 原路返回
local move_ease_in_back = move_ease_in:reverse()
-- 动作有块变慢
local move_ease_out = cc.EaseOut:create(createSimpleMoveBy(), 2.5)
-- 原路返回
local move_ease_out_back = move_ease_out:reverse()
-- 延时
local delay = createSimpleDelayTime()
-- 动作序列1
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
-- 动作序列2
local seq2 = cc.Sequence:create(move_ease_in,createSimpleDelayTime(),move_ease_in_back,createSimpleDelayTime())
-- 动作序列3
local seq3 = cc.Sequence:create(move_ease_out,createSimpleDelayTime(),move_ease_out_back,createSimpleDelayTime())
-- 执行一个无线循环的动作
local a2 = grossini:runAction(cc.RepeatForever:create(seq1))
a2:setTag(1)--改变这个用来更容易分辨节点的标记。
local a1 = tamara:runAction(cc.RepeatForever:create(seq2))
a1:setTag(1)
local a = kathia:runAction(cc.RepeatForever:create(seq3))
a:setTag(1)
-- 注册监听脚本
layer:registerScriptHandler(SpriteEase_onEnterOrExit)
Helper.titleLabel:setString("EaseIn - EaseOut - Stop")
return layer
end
-----------------------------------
--SpriteEaseInOut 精灵由慢至快再由快至慢
-----------------------------------
local function SpriteEaseInOut()
-- 获取基础层
local layer = getBaseLayer()
-- 创建移动动作
local move = createSimpleMoveBy()
-- 创建由慢变快再由快变慢的动作,第一个参数表示动作,第二个参数表示频率
local move_ease_inout1 = cc.EaseInOut:create(createSimpleMoveBy(), 0.65)
-- 返回执行与本Action对象相反操作的新Action对象
local move_ease_inout_back1 = move_ease_inout1:reverse()
-- 创建频率为1.35的easeInOut动作
local move_ease_inout2 = cc.EaseInOut:create(createSimpleMoveBy(), 1.35)
-- 返回执行与本Action对象相反操作的新Action对象
local move_ease_inout_back2 = move_ease_inout2:reverse()
-- 创建频率为1.0的easeInOut动作
local move_ease_inout3 = cc.EaseInOut:create(createSimpleMoveBy(), 1.0)
-- 返回执行与本Action对象相反操作的新Action对象
local move_ease_inout_back3 = move_ease_inout3:reverse()
-- 延时动作
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move_ease_inout1,delay,move_ease_inout_back1,createSimpleDelayTime())
local seq2 = cc.Sequence:create(move_ease_inout2,createSimpleDelayTime(),move_ease_inout_back2,createSimpleDelayTime())
local seq3 = cc.Sequence:create(move_ease_inout3, createSimpleDelayTime(), move_ease_inout_back3, createSimpleDelayTime() )
tamara:runAction(cc.RepeatForever:create(seq1))
kathia:runAction(cc.RepeatForever:create(seq2))
grossini:runAction(cc.RepeatForever:create(seq3))
Helper.titleLabel:setString("EaseInOut and rates")
return layer
end
-----------------------------------
--SpriteEaseExponential 精灵由慢至极快,由极快到慢
-----------------------------------
local function SpriteEaseExponential()
-- 得到基础层
local layer = getBaseLayer()
-- 创建移动动作
local move = createSimpleMoveBy()
-- 反操作
local move_back = move:reverse()
-- 创建由慢到极快的动作
local move_ease_in = cc.EaseExponentialIn:create(createSimpleMoveBy())
local move_ease_in_back = move_ease_in:reverse()
-- 创建有极快到慢的动作
local move_ease_out = cc.EaseExponentialOut:create(createSimpleMoveBy())
local move_ease_out_back = move_ease_out:reverse()
-- 创建延时动作
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime())
-- 三个丑八怪执行动作
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
kathia:runAction(cc.RepeatForever:create(seq3))
-- 设置层标题
Helper.titleLabel:setString("ExpIn - ExpOut actions")
return layer
end
-----------------------------------
--SpriteEaseExponentialInOut 精灵由慢至极快再由极快至慢
-----------------------------------
local function SpriteEaseExponentialInOut()
-- 得到基础层
local layer = getBaseLayer()
-- 创建简单移动动作
local move = createSimpleMoveBy()
-- 反向操作
local move_back = move:reverse()
-- 创建由慢至极快再由极快至慢的动作
local move_ease = cc.EaseExponentialInOut:create(createSimpleMoveBy())
local move_ease_back = move_ease:reverse()
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime() )
-- 设置两个丑八怪的位置
positionForTwo()
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
-- 设置层标题
Helper.titleLabel:setString("EaseExponentialInOut action")
return layer
end
-----------------------------------
--SpriteEaseSine 由快至慢、由慢至快
-----------------------------------
local function SpriteEaseSine()
-- 得到基础层
local layer = getBaseLayer()
-- 创建简单移动动作
local move = createSimpleMoveBy()
-- 反向操作
local move_back = move:reverse()
-- 创建由快至慢的动作
local move_ease_in = cc.EaseSineIn:create(createSimpleMoveBy())
local move_ease_in_back = move_ease_in:reverse()
-- 创建有慢至快的动作
local move_ease_out = cc.EaseSineOut:create(createSimpleMoveBy())
local move_ease_out_back = move_ease_out:reverse()
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() )
local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back,createSimpleDelayTime())
-- 运动了喂
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
kathia:runAction(cc.RepeatForever:create(seq3))
-- 设置标题
Helper.titleLabel:setString("EaseSineIn - EaseSineOut")
return layer
end
-----------------------------------
--SpriteEaseSineInOut 精灵由慢至快再由快至慢
-----------------------------------
local function SpriteEaseSineInOut()
-- 得到基础层
local layer = getBaseLayer()
-- 创建简单移动动作
local move = createSimpleMoveBy()
local move_back = move:reverse()
-- 由慢至快再由快至慢
local move_ease = cc.EaseSineInOut:create(createSimpleMoveBy())
local move_ease_back = move_ease:reverse()
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime())
-- 设置两个丑八怪的位置
positionForTwo()
-- 丑八怪运动啦
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
Helper.titleLabel:setString("EaseSineInOut action")
return layer
end
-----------------------------------
--SpriteEaseElastic
-----------------------------------
local function SpriteEaseElastic()
-- 得到基础层
local layer = getBaseLayer()
-- 创建简单移动动作
local move = createSimpleMoveBy()
local move_back = move:reverse()
-- 让目标动作赋予弹性 ,且以目标动作起点位子赋予弹性
local move_ease_in = cc.EaseElasticIn:create(createSimpleMoveBy())
local move_ease_in_back = move_ease_in:reverse()
-- 让目标动作赋予弹性 ,且以目标动作终点位子赋予弹性
local move_ease_out = cc.EaseElasticOut:create(createSimpleMoveBy())
local move_ease_out_back = move_ease_out:reverse()
-- 延迟动作
local delay = createSimpleDelayTime()
-- 创建动作序列
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() )
local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime())
-- 三个丑八怪又执行动作了
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
kathia:runAction(cc.RepeatForever:create(seq3))
-- 设置标题
Helper.titleLabel:setString("Elastic In - Out actions")
return layer
end
-----------------------------------
--SpriteEaseElasticInOut 让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性
-----------------------------------
local function SpriteEaseElasticInOut()
-- 创建基础层
local layer = getBaseLayer()
-- 创建简单移动的动作
local move = createSimpleMoveBy()
-- 让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性,频率0.3
local move_ease_inout1 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.3)
local move_ease_inout_back1 = move_ease_inout1:reverse()
-- 让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性,频率0.45
local move_ease_inout2 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.45)
local move_ease_inout_back2 = move_ease_inout2:reverse()
-- 让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性,频率0.6
local move_ease_inout3 = cc.EaseElasticInOut:create(createSimpleMoveBy(), 0.6)
local move_ease_inout_back3 = move_ease_inout3:reverse()
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move_ease_inout1, delay, move_ease_inout_back1, createSimpleDelayTime())
local seq2 = cc.Sequence:create(move_ease_inout2, createSimpleDelayTime(), move_ease_inout_back2, createSimpleDelayTime())
local seq3 = cc.Sequence:create(move_ease_inout3, createSimpleDelayTime(), move_ease_inout_back3, createSimpleDelayTime())
tamara:runAction(cc.RepeatForever:create(seq1))
kathia:runAction(cc.RepeatForever:create(seq2))
grossini:runAction(cc.RepeatForever:create(seq3))
Helper.titleLabel:setString("EaseElasticInOut action")
return layer
end
-----------------------------------
--SpriteEaseBounce
-----------------------------------
local function SpriteEaseBounce()
-- 得到基础层
local layer = getBaseLayer()
-- 创建简单移动动作
local move = createSimpleMoveBy()
local move_back = move:reverse()
-- 让目标动作缓慢开始
local move_ease_in = cc.EaseBounceIn:create(createSimpleMoveBy())
local move_ease_in_back = move_ease_in:reverse()
-- 让目标动作赋予反弹力,且以目标动作结束位子开始反弹
local move_ease_out = cc.EaseBounceOut:create(createSimpleMoveBy())
local move_ease_out_back = move_ease_out:reverse()
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime() )
local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime())
-- 烦不烦?
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
kathia:runAction(cc.RepeatForever:create(seq3))
Helper.titleLabel:setString("Bounce In - Out actions")
return layer
end
-----------------------------------
--SpriteEaseBounceInOut 让目标动作赋予反弹力,且以目标动作起始与结束位子开始反弹
-----------------------------------
local function SpriteEaseBounceInOut()
-- 得到基础层
local layer = getBaseLayer()
-- 创建简单移动动作
local move = createSimpleMoveBy()
local move_back = move:reverse()
-- 让目标动作赋予反弹力,且以目标动作起始与结束位子开始反弹
local move_ease = cc.EaseBounceInOut:create(createSimpleMoveBy())
local move_ease_back = move_ease:reverse()
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
local seq2 = cc.Sequence:create(move_ease, createSimpleDelayTime(), move_ease_back, createSimpleDelayTime())
positionForTwo()
-- 为什么只用两个丑八怪?
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
Helper.titleLabel:setString("EaseBounceInOut action")
return layer
end
-----------------------------------
--SpriteEaseBack
-----------------------------------
local function SpriteEaseBack()
local layer = getBaseLayer()
local move = createSimpleMoveBy()
local move_back = move:reverse()
-- 让目标动作赋予回力 , 且以目标动作起点位置作为回力点
local move_ease_in = cc.EaseBackIn:create(createSimpleMoveBy())
local move_ease_in_back = move_ease_in:reverse()
-- 让目标动作赋予回力 , 且以目标动作终点位置作为回力点
local move_ease_out = cc.EaseBackOut:create(createSimpleMoveBy())
local move_ease_out_back = move_ease_out:reverse()
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
local seq2 = cc.Sequence:create(move_ease_in, createSimpleDelayTime(), move_ease_in_back, createSimpleDelayTime())
local seq3 = cc.Sequence:create(move_ease_out, createSimpleDelayTime(), move_ease_out_back, createSimpleDelayTime())
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
kathia:runAction(cc.RepeatForever:create(seq3))
Helper.titleLabel:setString("Back In - Out actions")
return layer
end
-----------------------------------
--SpriteEaseBackInOut 让目标动作赋予回力 , 且以目标动作起点和终点位置作为回力点
-----------------------------------
local function SpriteEaseBackInOut()
local layer = getBaseLayer()
local move = createSimpleMoveBy()
local move_back = move:reverse()
-- 让目标动作赋予回力 , 且以目标动作起点和终点位置作为回力点
local move_ease = cc.EaseBackInOut:create(createSimpleMoveBy())
local move_ease_back = move_ease:reverse()
local delay = createSimpleDelayTime()
local seq1 = cc.Sequence:create(move, delay, move_back, createSimpleDelayTime())
local seq2 = cc.Sequence:create(move_ease,createSimpleDelayTime(), move_ease_back, createSimpleDelayTime())
positionForTwo()
grossini:runAction(cc.RepeatForever:create(seq1))
tamara:runAction(cc.RepeatForever:create(seq2))
Helper.titleLabel:setString("EaseBackInOut action")
return layer
end
-----------------------------------
--SpeedTest 速度测试
-----------------------------------
local SpeedTest_entry = nil
local SpeedTest_action1 = nil
local SpeedTest_action2 = nil
local SpeedTest_action3 = nil
local function altertime(dt)
-- 设置速度
SpeedTest_action1:setSpeed(math.random() * 2)
SpeedTest_action2:setSpeed(math.random() * 2)
SpeedTest_action3:setSpeed(math.random() * 2)
end
local function SpeedTest_onEnterOrExit(tag)
if tag == "enter" then
SpeedTest_entry = scheduler:scheduleScriptFunc(altertime, 1.0, false)
elseif tag == "exit" then
scheduler:unscheduleScriptEntry(SpeedTest_entry)
end
end
-- 速度测试
local function SpeedTest()
local layer = getBaseLayer()
--参数1:跳到目标动作位子的所需时间
--参数2:目标位置
--参数3:跳的高度
--参数4跳到目标位置的次数
local jump1 = cc.JumpBy:create(4, cc.p(- s.width + 80, 0), 100, 4)
local jump2 = jump1:reverse() -- 反向动作
-- 旋转动作,参数1:旋转的时间 参数2:旋转的角度 0 - 360
local rot1 = cc.RotateBy:create(4, 360 * 2)
local rot2 = rot1:reverse()
-- 蹦蹦跳跳的动作序列
local seq3_1 = cc.Sequence:create(jump2, jump1)
-- 旋转的动作序列
local seq3_2 = cc.Sequence:create(rot1, rot2)
-- 让多个动作同时执行
local spawn = cc.Spawn:create(seq3_1, seq3_2)
-- 速度动作1
SpeedTest_action1 = cc.Speed:create(cc.RepeatForever:create(spawn), 1.0)
-- 克隆一个spawn
local spawn2 = spawn:clone()
-- 速度动作2
SpeedTest_action2 = cc.Speed:create(cc.RepeatForever:create(spawn2), 1.0)
local spawn3 = spawn:clone()
-- 速度动作3
SpeedTest_action3 = cc.Speed:create(cc.RepeatForever:create(spawn3), 1.0)
-- 最后一个了
grossini:runAction(SpeedTest_action2)
tamara:runAction(SpeedTest_action3)
kathia:runAction(SpeedTest_action1)
layer:registerScriptHandler(SpeedTest_onEnterOrExit)
Helper.titleLabel:setString("Speed action")
return layer
end
function EaseActionsTest()
-- 创建场景
local scene = cc.Scene:create()
cclog("EaseActionsTest")
-- 创建方法表
Helper.createFunctionTable = {
SpriteEase,
SpriteEaseInOut,
SpriteEaseExponential,
SpriteEaseExponentialInOut,
SpriteEaseSine,
SpriteEaseSineInOut,
SpriteEaseElastic,
SpriteEaseElasticInOut,
SpriteEaseBounce,
SpriteEaseBounceInOut,
SpriteEaseBack,
SpriteEaseBackInOut,
SpeedTest
}
-- 添加子节点
scene:addChild(SpriteEase())
-- 添加返回菜单
scene:addChild(CreateBackMenuItem())
return scene
end