码迷,mamicode.com
首页 > 其他好文 > 详细

基于dragonbones的cocos2dx lua封装

时间:2015-12-09 13:25:33      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

 

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

 

--- 骨骼动画包装器
-- 建议最多包括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

 

基于dragonbones的cocos2dx lua封装

标签:

原文地址:http://www.cnblogs.com/ColaZhang/p/5032257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!