最近項目有用到播放一個骨骼動畫,播放完成後,啟動另外一個動畫,在此使用骨骼動畫的回調函數實作。過程如下:
一、實作動畫播放結束後的回調
、得到動畫,播放動畫,并設定動畫的回調函數
local armature = self._viewNode.amture(自己定義的骨骼動畫)
armature:getAnimation():play("hall_light")
--(設定動畫回調函數animationEvent)
armature:getAnimation():setMovementEventCallFunc(animationEvent)
、動畫回調函數的實作
在此屏蔽動畫開始事件:ccs.MovementEventType.start
animationEvent = function(armatureBack, movementType, movementID)
if movementType == ccs.MovementEventType.start then
return
end
if armatureBack:isVisible() == true then
armatureBack:stopAllActions()
local delayTime = cc.DelayTime:create()
local action = cc.Sequence:create(delayTime,cc.CallFunc:create(callback1))
armatureBack:runAction(action)
end
end
、動畫結束後執行的函數
function callback1()
--執行動畫結束後的操作
end
二、停止動畫回調
function stopArmatureCallBack()
amrature:stopAllActions()
amrature:getAnimation():setMovementEventCallFunc(function() end)
amrature:getAnimation():setMovementEventCallFunc(assert)
end
三、代碼完整展現
實作動畫2s循環播放
function MainCtrl:startNeonLightAni()
local animationEvent = nil
local function callback1()
armature:setVisible(true)
armature:getAnimation():play("hall_light")
end
animationEvent = function(armatureBack, movementType, movementID)
if movementType == ccs.MovementEventType.start then
return
end
if armatureBack:isVisible() == true then
armatureBack:stopAllActions()
armatureBack:setVisible(false)
local delayTime = cc.DelayTime:create()
local action = cc.Sequence:create(delayTime,cc.CallFunc:create(callback1))
armatureBack:runAction(action)
end
end
armature:setVisible(true)
armature:getAnimation():play("hall_light")
armature:getAnimation():setMovementEventCallFunc(animationEvent)
end
停止動畫的播放
function MainCtrl:stopNeonLightAni()
if armature then
armature:setVisible(false)
armature:stopAllActions()
armature:getAnimation():setMovementEventCallFunc(function() end)
armature:getAnimation():setMovementEventCallFunc(assert)
end
end