基于dragonbones的cocos2dx lua封装

本文介绍了一个基于DragonBones框架的骨骼动画封装方法,通过Lua脚本实现了动画资源的加载与管理、动画的播放控制及回调事件注册等功能,并提供了详细的API示例。

 

个别本地代码可能编不过去,自己改一下吧

 

--- 骨骼动画包装器
-- 建议最多包括2级嵌套动画

local Animation = {}

Animation.Z_ORDER_UPDATED = 0
Animation.ANIMATION_FRAME_EVENT = 1
Animation.BONE_FRAME_EVENT = 2
Animation.SOUND = 3
Animation.FADE_IN = 4
Animation.FADE_OUT = 5
Animation.START = 6
Animation.COMPLETE = 7
Animation.LOOP_COMPLETE = 8
Animation.FADE_IN_COMPLETE = 9
Animation.FADE_OUT_COMPLETE = 10
Animation._ERROR = 11

--- 创建一个动画并返回
-- @string fileName    动画名
-- @string armatureName 骨架名
-- @return NodeAni
function Animation.node(fileName, armatureName)
    return require("cola.ui.NodeAni").new(fileName, armatureName)
end

--- 删除已加载的指定名字的动画资源缓存
-- @string fileName 文件名
function Animation.removeRes(fileName)
    local factory = db.DBCCFactory:getInstance()
    local textureName = Animation.getTexturePng(fileName)
    factory:removeDragonBonesData(fileName)
    factory:removeTextureAtlas(fileName)
    --cocos2dx又换存了一次  需要清空否则 图片纹理不回收  实测
    TextureCache:removeTextureForKey(textureName)
end

--- 清空所有加载过的Ani资源
-- 全部卸载干净 一般用于游戏热更新后重启
function Animation.removeAllRes()
    local factory = db.DBCCFactory:getInstance()
    factory:dispose(true)
end

--- 返回指定名字的动画骨架XML
-- @string fileName 文件名
function Animation.getSkeletonXml(fileName)
    return "res/ani/" .. fileName .. "/skeleton.xml"
end

--- 返回指定名字的动画图片XML
-- @string fileName 文件名
function Animation.getTextureXml(fileName)
    return "res/ani/" .. fileName .. "/texture.xml"
end

--- 返回指定名字的动画图片png
-- @string fileName 文件名
function Animation.getTexturePng(fileName)
    return "res/ani/" .. fileName .. "/texture.png"
end

return Animation

 --- 动画封装node

-- 基于dragonbones开源骨骼框架
-- 建议嵌套子动画不要超过2级

local NodeAni = class("NodeAni", function(...)
    return d.node() end) --- 构造方法 -- @string fileName 文件名 -- @string armatureName 骨架名 function NodeAni:ctor(fileName, armatureName) self.__fileName = fileName local factory = db.DBCCFactory:getInstance() self.__factory = factory local skeletonName = Animation.getSkeletonXml(fileName) local textureName = Animation.getTextureXml(fileName) factory:loadDragonBonesData(skeletonName, fileName) factory:loadTextureAtlas(textureName, fileName) if (armatureName) then self:setArmatureName(armatureName) end end --- 播放指定动作的动画 -- @string actionName 动作名 默认值为"a" -- @treturn self function NodeAni:play(actionName) assert(self.__armature, "@NodeAni play error! must setArmatureName first!!") self.__actionName = actionName or "a" self.__armature:getAnimation():gotoAndPlay(self.__actionName) return self end --- 停止动画并恢复到初始状态 -- @treturn self function NodeAni:stop() assert(self.__armature, "@NodeAni play error! must setArmatureName first!!") if self.__actionName then self.__armature:getAnimation():gotoAndStop(self.__actionName, 0, 0) end return self end --- 设置动画结束时执行回调方法 -- 循环动画不会触发回调 -- @func func 结束时回调方法 -- @treturn self function NodeAni:setEnded(func) self.__endedFunc = func self:_registerAnimationEvent() return self end --- 移除动画结束时的回调方法 -- @treturn self function NodeAni:removeEnded() self.__endedFunc = nil return self end --- 设置循环动画时每一次结束时的回调方法 -- 非循环动画不会触发回调, 循环动画回调会触发多次 -- @func func 每一次结束时回调方法 -- @treturn self function NodeAni:setLoopEnded(func) self.__loopEndedFunc = func self:_registerAnimationEvent() return self end --- 移除循环动画每一次结束时的回调方法 -- @treturn self function NodeAni:removeLoopEnded() self.__loopEndedFunc = nil return self end --- 设置骨架 -- @string armatureName 骨架名字 -- @treturn self function NodeAni:setArmatureName(armatureName) if (self.__armature) then self.__armature:removeFromParent() self.__armature = nil end self.__armatureName = armatureName local armature = self.__factory:buildArmatureNode(armatureName):addTo(self) self.__armature = armature return self end --- 设置骨骼所在的节点为新的node -- @string boneName 骨骼名字 -- @tparam cc.Node node 新的node -- @treturn self function NodeAni:setBoneNode(boneName, node) assert(self.__armature and boneName and node, "@NodeAni setBoneNode error!") local slot = self.__armature:getCCSlot(boneName) local oldDisplay = slot:getCCDisplay() local anchorPoint if (oldDisplay) then anchorPoint = oldDisplay:getAnchorPoint() else anchorPoint = cc.p(0.5, 0.5) end node:setAnchorPoint(anchorPoint) node:retain() slot:setDisplayImage(node) return self end --- 设置子骨骼所在的节点为新的node -- @string parentBoneName 子动画所在的父骨骼名 -- @string childBoneName 子动画的子骨骼名 -- @tparam cc.Node node 新的node -- @treturn self function NodeAni:setChildBoneNode(parentBoneName, childBoneName, node) assert(self.__armature and parentBoneName and childBoneName and node, "@NodeAni setChildBoneNode error!") local slot = self.__armature:getCCSlot(parentBoneName) local armature = slot:getCCChildArmature() assert(armature, "@NodeAni getChildArmature error") local childSlot = armature:getCCSlot(childBoneName) local oldDisplay = childSlot:getCCDisplay() local anchorPoint if (oldDisplay) then anchorPoint = oldDisplay:getAnchorPoint() else anchorPoint = cc.p(0.5, 0.5) end node:setAnchorPoint(anchorPoint) node:retain() childSlot:setDisplayImage(node) return self end function NodeAni:_registerAnimationEvent() assert(self.__armature, "@NodeAni addEventListener error! must setArmatureName first!!") if not self.__armature._isRegisterAnimation then self.__armature._isRegisterAnimation = true self.__armature:registerAnimationEventHandler(function(event) if event.type == Animation.COMPLETE and self.__endedFunc then self.__endedFunc(event) elseif event.type == Animation.LOOP_COMPLETE and self.__loopEndedFunc then self.__loopEndedFunc(event) end end) end return self end return NodeAni

 

转载于:https://www.cnblogs.com/ColaZhang/p/5032257.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值