cocos lua 的Armature动画之节点的替换功能

2018-01-08  本文已影响0人  GCKJianshu

前言

做cocos lua开发的很多都吐槽cocos 的文档不好,特别是因为cocos版本太多,而且很多项目版本可能比较古老,一些api查阅起来很困难。之前在项目中遇到一个动画,需要替换Armature当中一直数字的节点,在framework目录下搜索了一下armature,意外地发现了cocos lua的api说明,因为项目用的事quik-cocos2dx 3.3,我看了一下cocos2dx 3.10版本的东西,发现同样有这么一个api说明。大体目录为:frameworks\cocos2d-x\cocos\scripting\lua-bindings\auto\api。如果你的项目中没有就直接搜索api,应该就能找到你想要的东西,这个api说明对开发还是有不小的帮助的。

动画节点替换

直接上代码

 ccs.ArmatureDataManager:getInstance():addArmatureFileInfo("PPBYyuquanxiao/PPBYyuquan.ExportJson")  -- 加载动画文件
 local fishAnimation = ccs.Armature:create("PPBYyuquan")  -- 创建动画
 fishAnimation:getAnimation():playWithIndex(0, -1, 0)        -- 播放动画
 fishAnimation:setPosition(cc.p(0, 0))    -- 设置动画位置

cocos lua 为armature的节点替换提供了一些接口的,具体可以参考上面提到的api当中的Armature说明。
这里用到的就简单的三个getBone, addDisplay, changeDisplayWithIndex

-- 创建一个数字的label
local atlasLabel = cc.LabelAtlas:_create("0", fishJoyPath.resPath.."image/shuziquan.png", 44, 76, string.byte('0'))
atlasLabel:setString(tostring(value))
local numSize = atlasLabel:getContentSize()
atlasLabel:setPosition(cc.p(-numSize.width/2, -numSize.height/2))
-- 替换节点
local shuzi = fishAnimation:getBone("4shuzi")
shuzi:addDisplay(atlasLabel, 1)
shuzi:changeDisplayWithIndex(1, true)
parent:addChild(fishAnimation)

通过执行上面的代码,就可以把一个动画中名字为“4shuzi”的节点替换成任何你想要的数字,并且表现形式和做出来的动画一样。

这里参考了简书作者的一篇文章,他的文中可能说得更加详细一些Cocos2d-x中替换动画(Armature)中的节点与粒子

上一篇下一篇

猜你喜欢

热点阅读