azerion / phaser-spine

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

How I can change the skin only of one slot? #56

Closed TinyDobbins closed 7 years ago

TinyDobbins commented 7 years ago

for example I can the character with different weapons, how I can change the slot "gun"?

I tried this: hero.skeleton.setAttachment("weapon", "weapon2");

but this slot auto back to 'weapon1' after the animation is completed.

shibekin69 commented 7 years ago

This is because whenever you change your skin, your animated character's setting always goes back to whatever you set in the Setup Pose. One way of overriding this is that, you need to program something in to apply to the skeleton's attachment:

To show it: SpineCharacter.skeleton.setAttachment(slotName, attachmentName)

To hide it: SpineCharacter.skeleton.setAttachment(slotName, null)

But to make this work, all your skin attachments need to appear in the slot (so you'll need to duplicate this from the skin slot if needed). This is because the above code will look for these attachments and set it, overriding or adding it on top of your skin settings. For example,

What your tree looks like when you've made your Weapon 1 skin and you activate the skin in the editor:

[Weapon Slot A]
    [Weapon Slot A SkinPlaceholder]
        [Weapon 1 Attachment]

What your tree should look like to make this trick work when you activate the skin in the editor:

[Weapon Slot A]
    [Weapon 1 Attachment]
    [Weapon 2 Attachment]
    [Weapon 3 Attachment]
    [Weapon Slot A SkinPlaceholder]
        [Weapon 1 Attachment]

Now assuming that you don't need the above and all your guns are available as skins, you can just switch skins. But the problem with skins is that it's limited to a certain preset of attachments you tell it before hand. And if you have many different things changing up, making a ton of combinedskins for all those combinations is crazy. So the above workaround is one trick you can do. You may have to set up your animations properly to make this work though.

TinyDobbins commented 7 years ago

@shibekin69 thanks a lot for help! very detailed answer.