• 基于dragonbones的cocos2dx lua封装


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

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

     

  • 相关阅读:
    第六周作业:《人月神话》对我做项目实践的启示(一)
    第五周作业:网站的初步设计
    关于做团队项目时需求分析工作中所学的一部分知识
    软件工程学生的编程能力与编程语言是中文或英文有关系吗?
    面向过程(或者叫结构化)分析方法与面向对象分析方法到底区别在哪里?请根据自己的理解简明扼要的回答。
    当下大部分互联网创业公司为什么都愿意采用增量模型来做开发
    1+X Web前端开发(中级)理论考试样题(附答案)
    1+X Web前端开发(初级)理论考试样题(附答案)
    vi 和vim 的区别
    Linux查看日志三种命令
  • 原文地址:https://www.cnblogs.com/ColaZhang/p/5032257.html
Copyright © 2020-2023  润新知