azerion / phaser-spine

A plugin for Phaser 2 that adds Spine support
MIT License
121 stars 57 forks source link

[Spine-TS] CreateCombinedSkin Not Working #71

Closed shibekin69 closed 6 years ago

shibekin69 commented 6 years ago

Seems like this function likes to give me this error in the console:

something went wrong with reading the attachments index and/or name

I'm not sure what the problem with this one is tho.

EDIT: I looked into this. It seems attachmentName is 'undefined'

You can test it with this:

skin = skeleton.data.findSkin('ASkinNameHere')

for (var key in skin.attachments) {
    var slotKeyPair = key.split(':');
    console.log('slotKeyPair');
    console.log(slotKeyPair);  //Returns ["10"]

    var slotIndex = parseInt(slotKeyPair[0]);
    console.log('slotIndex'); 
    console.log(slotIndex);  // Returns 10

    var attachmentName = slotKeyPair[1];
    console.log('attachmentName');  
    console.log(attachmentName); //Returns undefined

    var attachment = skin.attachments[key];
    console.log('attachment');
    console.log(attachment); //Returns RegionAttachment Object

    if (undefined === slotIndex || undefined === attachmentName) {
        console.warn('something went wrong with reading the attachments index and/or name');
    }
}
shibekin69 commented 6 years ago
I must've forgotten to paste the solution (at least for me) here. This is for the createCombinedSkin() function:

  Spine.prototype.createCombinedSkin = function (newSkinName) {
      var skinNames = [];
      for (var _i = 1; _i < arguments.length; _i++) {
          skinNames[_i - 1] = arguments[_i];
      }
      if (skinNames.length === 0) {
          console.warn('Unable to combine skins when no skins are passed...');
          return;
      }
      var newSkin = new spine.Skin(newSkinName);
      for (var i = 0; i < skinNames.length; i++) {
          var skinName = skinNames[i];
          var skin = this.skeleton.data.findSkin(skinName);
          if (!skin) {
              console.warn("Skin not found: " + skinName);
              return;
          }
          for (var key in skin.attachments) {
              var slotKeyPair = key.split(':');
              var slotIndex = parseInt(slotKeyPair[0]);
              //var attachmentName = slotKeyPair[1];
              //CHANGE
              var attachmentName = Object.keys(skin.attachments[key])[0];
              //var attachment = skin.attachments[key];
              //CHANGE
              var attachment = skin.attachments[key][attachmentName];
              console.log(key);
              console.log(attachment);
              if (undefined === slotIndex || undefined === attachmentName) {
                  console.warn('something went wrong with reading the attachments index and/or name');
                  return;
              }
              if (newSkin.getAttachment(slotIndex, attachmentName) !== undefined) {
                  //CHANGE: IMPORTANT! COMMENT THESE OUT! OR ELSE WON'T SEE COMBINED SKINS.
                  //console.warn('Found double attachment for: ' + skinName + '. Skipping');
                  //continue;
              }
              newSkin.addAttachment(slotIndex, attachmentName, attachment);
              console.log(newSkin);
          }
      }
      this.skeleton.data.skins.push(newSkin);
      return newSkin;
  };
AleBles commented 6 years ago

I think you pasted it here: https://github.com/orange-games/phaser-spine/issues/70#issuecomment-323509123 :p

shibekin69 commented 6 years ago

This seems to work :)