目前项目中碰到需要连续播放动画的功能(技能combo)
在cocosstudio 里面做了一连串数字显示的动画
播放的时候发现获取到的值是一个比较大的数(例如10),显示得从动画1显示到10
因为目前combo只显示到2位数,因此分为显示1位数的动画,显示两位数的动画和显示combo图标的动画三种情况。
这里面从返回的数值(返回的数值是需要显示的combo值)得到需要到的数值,最终发现需要连续显示从1到该combo值的动画。使用的就是动画播放里面的playWithNames方法
playWithNames方法属于ArmatureAnimation 类下面的方法,具体可以参照cocos2dx API文档http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d8/d5d/classcocos2d_1_1extension_1_1armature_1_1_armature_animation.html
playWithNames(indextemp,index,bool)中有三个参数,第一个参数是动画文件名称的table,第二个是要播放到动画文件的index值,也就是动画文件table中需要播放到的索引值为整型,第三个bool值是是否循环的bool值。
下面上lua代码:
if self.armature then self.armature:removeFromParent() end
if self.armatureCombo then self.armatureCombo:removeFromParent() end
if self.armatureGewei then self.armatureGewei:removeFromParent() end
-- if tonumber(Num)<10 then--combo是1位数
--Num=25
ccs.ArmatureDataManager:getInstance():addArmatureFileInfo("effect/combonumber/combonumber0.png","effect/combonumber/combonumber0.plist","effect/combonumber/combonumber.ExportJson"); -- 加载动画所用到的数据
if tonumber(Num)<10 then--combo是1位数
self.armature = ccs.Armature:create("combonumber"); -- 创建动画对象
--armature:setPosition(0,200);
self.armature:setPosition(cc.p(1084+1+52,348+56))
local index = tonumber(Num)
local indextemp={}
for k=1,index do
local name="0"..k
if k == index then name = "0"..k.."_lst" end --最后一个动画结尾不消失
table.insert(indextemp,name)
end
self.armature:getAnimation():playWithNames(indextemp,index,false);
local speed = self.armature:getAnimation():getSpeedScale()
self.armature:getAnimation():setSpeedScale(index)
print("播放动画速度"..speed.."index=="..index)
print("播放动画完毕1")
--armature:getAnimation():playWithIndexes(indextemp);
-- armature:getAnimation():playWithIndex(0,index,0); -- 设置动画对象执行的动画名称
elseif tonumber(Num)>=10 then
local numGeWEI =tonumber(Num)%10 --个位
local numShiWei = (tonumber(Num)-numGeWEI)/10 --十位
self.armature = ccs.Armature:create("combonumber"); -- 创建十位动画对象
--armature:setPosition(0,200);
self.armature:setPosition(cc.p(1084+1+52,348+56))
local index = tonumber(Num)
local indextempten={}
for k=1,index do
local name="0".. math.floor(k/10)
print("shiwei shi "..name)
if k == index then name = "0"..numShiWei.."_lst" end --最后一个动画结尾不消失
table.insert(indextempten,name)
end
self.armature:getAnimation():playWithNames(indextempten,index,false);
local speed = self.armature:getAnimation():getSpeedScale()
self.armature:getAnimation():setSpeedScale(index)
print("播放动画速度"..speed.."index=="..index)
print("播放动画完毕2")
self.armatureGewei = ccs.Armature:create("combonumber"); -- 创建个位动画对象
--armature:setPosition(0,200);
self.armatureGewei:setPosition(cc.p(1084+1+52,348+56))
local armaturename= "0"..numGeWEI -- 设置位置
--armature:getAnimation():play(armaturename);
local indexge = tonumber(Num)
local indextempge={}
for k=1,indexge do
local ge = k%10
local name="double_"..ge
print("gewei".. name)
-- if k == index then name = "0"..numGeWEI.."_lst" end --最后一个动画结尾不消失
table.insert(indextempge,name)
end
self.armatureGewei:getAnimation():playWithNames(indextempge,indexge,false);
local speedge = self.armatureGewei:getAnimation():getSpeedScale()
self.armatureGewei:getAnimation():setSpeedScale(indexge)
print("播放动画速度"..speedge.."index=="..indexge)
print("播放动画完毕3")
end
self.armatureCombo = ccs.Armature:create("combonumber"); -- 创建combo字动画对象
--armature:setPosition(0,200);
if Num<10 then
self.armatureCombo:setPosition(cc.p(1084,348+56))
else
self.armatureGewei:setPositionX(1084)
self.armature:setPositionX(1084)
self.armatureCombo:setPosition(cc.p(1084,348+56))
end
local index = tonumber(Num)
local combo={}
for k=1,index do
local name="comboword"
table.insert(combo,name)
end
self.armatureCombo:getAnimation():playWithNames(combo,index,false);
local speed = self.armatureCombo:getAnimation():getSpeedScale()
self.armatureCombo:getAnimation():setSpeedScale(index)
print("播放动画速度"..speed.."index=="..index)
print("播放动画完毕4")
if self.armatureGewei then self:addChild(self.armatureGewei,1000) end
if self.armature then self:addChild(self.armature,1000) end
--self:addChild(self.armature,1000)
self:addChild(self.armatureCombo,1000);
中间发现需要调节动画的播放速度,使用的是<span style="color:#ff0000;">self.armatureCombo:getAnimation():setSpeedScale(index)</span>方法,不设置的话默认是1。
最终主要用的是连续播放动画方法
<span style="color:#ff0000;">getAnimation():playWithNames(indextemp,index,false);</span>

本文介绍如何使用Cocos2d-x实现连续动画播放功能,特别关注技能combo动画的展示。文中提供了Lua代码示例,展示了如何根据不同数量级播放不同组合的动画,并调整动画播放速度。
1121

被折叠的 条评论
为什么被折叠?



