@elor 写道:
在试验创建新皮肤换装的功能,更改了spine官方的一个例子,想要实现随机切换皮肤,但是总是报错
Uncaught Skin not found: [object Object] spine.js:1159
或者 Uncaught Skin not found: random-skin想请问 cocos动态创建spine皮肤并且换上的流程应该是怎么样的?
======================================================
代码如下:
randomizeAttachments: function () { var skins = []; for (var skin in this.spData.skeletonData.skeletonJson.skins) { if (skin === 'default') continue; skins.push(this.spData.skeletonData.skeletonJson.skins[skin]); } var newSkin = new sp.spine.Skin('random-skin'); for (var slot = 0; slot < this.spData.skeletonData.skeletonJson.slots.length; slot ++) { var skin = skins[(Math.random() * skins.length - 1) | 0]; var slotname = this.spData.skeletonData.skeletonJson.slots[slot].name; cc.log (JSON.stringify(skin)) cc.log('name:'+ skin.name); var attachments = skin[slotname]; for (var attachmentName in attachments) { newSkin.addAttachment(slot, attachmentName, attachments[attachmentName]); } } cc.log(JSON.stringify(newSkin)); this.spData.setSkin(newSkin); },
====================================
发现有2个问题
一个是newSkin.addAttachment创建出来的皮肤数据的格式和官方皮肤格式不一样?
cocos源码里面的addAttachment是这样:
spine.Skin.prototype = {
addAttachment: function (slotIndex, name, attachment) {
this.attachments[slotIndex + ":" + name] = attachment;
},用skin.addAttachment生成的皮肤数据是这样的:
{
"name": "random-skin",
"attachments": {
"0:upper_far": {
"name": "Fletch/Fletch_arm_upper_far",
"x": 36.95,
"y": 6,
"rotation": 89.14,
"width": 295,
"height": 278
},spine文件的json里面格式是这样的:
"Assassin": {
"arm_lower_far": {
"arm_lower_far": {
"name": "Assassin/Assassin_arm_lower_far",
"x": 44.7,
"y": 6.2,
"rotation": 92.05,
"width": 259,
"height": 209
}
},不知道和这个格式有没有关系
另一个是setSkin(newSkin)从代码跟踪到的都是设置skinname而不是文件
而用 setSkin(newSkin.name)还是找不到
帖子: 1
参与者: 1