Tutorial

Page 8 of 10

Attachments

As mentioned previously, you can "attach" images to the skeletal animation sprite and they will behave as if they were part of the bone structure of the animation. To do this in GameMaker: Studio you simply use the "slot" name to hold the attachment and the attachment name to assign to that slot. These names are defined in Spine when you are creating your skeletal animation, and the attachments are normally included in the Spine files.

However, since attachments are simple graphic assets that don't have a bone assigned to them, you also have the ability to use one of the sprite resources that are bundle with your game. This can be a sprite which is part of the base resources for your game, or as an included file and it's this which makes Spine an even more flexible option when creating sprites.

Showing And Changing Attachments

The Spine sprite that we are using has three attachments included with it, and it has an attachment slot for the "right" hand of the sprite character. So, we are going to do the same as we did for the skins and create a 1D array to hold the attachment names and then skip through them using a key press.

First, in the "oPlayer" Create Event, add the following code:



This sets up the array of "weapons" that we want to attach, creates the controller variable and also assigns a weapon to the right hand slot. Like the previous functions for skins and animation sets, the attachment functions are based on the names that you have given the slots and attachments in Spine. It is worth noting that we don't have a string for the fourth weapon in the array, but rather a value of -1. this is used to set the attachment to "empty", so when we reach that one, the sprite should have no attachment visible.

Now add a Key Press Event for the "W" key and in it place the following code:



Run the game and change the attachments by pressing the "W" key on the keyboard. Note that if you also change skins (press "Q"), then the default attachment for that skin will be set, which in this case is no attachment, and so the weapon will disappear.

Sprite Attachments

Apart from those images that you include with your Spine file on import, you can also attach any other sprite from your game to a skeletal animation. This means that you can have a single animation, and then assign custom, player-made sprites to it, or have DLC that the player can download and use etc... However for this to work correctly, the sprite must first be "flagged" as an attachment and given a unique name using the appropriate function.

You may have noticed that we have a rather large sprite in the resource tree called "spr_Sword". this is the sprite that we are going to use as a custom attachment for out tutorial game. So, in the "oPlayer" Create Event add the following code after everything else:



Now, it is important to note that the x/y origin of the sprite in the UI has nothing to do with where it is positioned on the skeleton. The sprite will be be placed as if it's origin was at the centre of the image no matter what, and so you must use the offset arguments in this function to position it correctly. In this case we want to set the offset to 250 and the angle to 90 (experiment with other values to get used to how this works).

Since we have assigned our sword to slot [3] in the array, we can go ahead and test the game again, this time you should see that the sword is indeed attached to the player hand at the correct place, and that it moves along with the hand and arm while walking and jumping.



© Copyright YoYo Games Ltd. 2014 All Rights Reserved