在cocos2d-x 3.15中可以通过序列帧来实现动画效果,具体实现方法就不说了。
不过,在这个过程中有个地方需要注意,就是序列帧动画大小的设置,如下图所示的情况
要想让动画跟 背景边框完全重合的话,需要设置缩放来适应,代码如下
function Active_SignInLayer:createAnimation( backSprite,count )
local animation = cc.Animation:create()
local backSize = backSprite:getContentSize()
local aniSprite
if count < 7 then--创建第一天到第六天的动画
for j=1,9,1 do
local name = "Active/animation_oneToSix_"..j..".png"
local fileName = G_GetPlazaRes(name)
animation:addSpriteFrameWithFile(fileName)
end
aniSprite = cc.Sprite:create(G_GetPlazaRes("Active/animation_oneToSix_1.png"))
else--创建第七天的动画
for j=1,9,1 do
local name = "Active/animation_seven_"..j..".png"
local fileName = G_GetPlazaRes(name)
animation:addSpriteFrameWithFile(fileName)
end
aniSprite = cc.Sprite:create(G_GetPlazaRes("Active/animation_seven_1.png"))
local aniSize = aniSprite:getContentSize()
end
--计算缩放值
local aniSize = aniSprite:getContentSize()
local sx = backSize.width/(aniSize.width-6)
local sy = backSize.height/(aniSize.height-6)
aniSprite:addTo(backSprite)
aniSprite:setAnchorPoint(0.5,0.5)
aniSprite:setPosition(backSize.width/2,backSize.height/2)
-- aniSprite:setContentSize(backSize)
--缩放
aniSprite:setScaleX(sx)
aniSprite:setScaleY(sy)
animation:setDelayPerUnit(0.08)
animation:setRestoreOriginalFrame(true)--动画执行后还原初始状态
local action = cc.Animate:create(animation)
aniSprite:runAction(cc.RepeatForever:create(action))
print("sx = " .. sx..";sy = " .. sy)
end