先看一下CocosCreator工程结构,如下图:
目标是点击右上角的按钮,往ActorLayer添加一个Dragon动画,代码如下:
cc.Class({ extends: cc.Component,properties: { },// use this for initialization onLoad: function () { },// called every frame,uncomment this function to activate update callback // update: function (dt) { // },onClick_add:function(){ var self = this; cc.loader.loadResAll('Dragon',function(err,assets){ if(err){ return; } if(assets.length <= 0){ return; } var newHero = new cc.Node(); self.node.addChild(newHero); newHero.setPosition(cc.p(0,0)); newHero.setScale(0.5,0.5); var dragonDisplay = newHero.addComponent(dragonBones.ArmatureDisplay); for(var i in assets){ if(assets[i] instanceof dragonBones.DragonBonesAsset){ dragonDisplay.dragonAsset = assets[i]; } if(assets[i] instanceof dragonBones.DragonBonesAtlasAsset){ dragonDisplay.dragonAtlasAsset = assets[i]; } } dragonDisplay.armatureName = 'Dragon'; dragonDisplay.playAnimation('stand'); }) } });
注意点:
1.dragonBones.ArmatureDisplay的dragonAsset属性和dragonAtlasAsset属性必须赋值loadRes加载到的对象;
2.使用cc.loader.loadResAll动态加载资源,被加载资源必须放到resources目录下面;
参考:
http://forum.cocos.com/t/topic/41362
原文链接:https://www.f2er.com/cocos2dx/338972.html