Tutorial
Page 9 of 10
Manually Setting Bones
We are nearly finished this tutorial for skeletal animations, but we are missing one important part of using them in GameMaker: Studio, and that is the ability to change any bones position manually.
All Spine skeletal animations are made up of key-frames which are then interpolated between. this means that each step of your game, all the animations bones have their position updated just before being drawn
to the screen. What this means is that if we can somehow intercept this data and change it ourselves just before drawing then we can set the bones to be however we want.
The Animation Update Event
For Spine skeletal animations (and only this type of animation) there is a special event in the Other category - the Animation Update Event. This event is triggered every single step for an
instance with a Spine sprite assigned to it, and it occurs just after all the bones of the skeleton have been set to their position for drawing. This means that we can use this event to change the values of a bone, and
ensure that it will be drawn with these new values.
Add this event to your player object now, then copy the following code (we will explain this next):
In order to change a bone, we first need to create a ds_map and then populate it with the bone information. This is done using the skeleton_bone_state_get function, and the map will contain
the following keys:
- "x": The local x position of the bone relative to the parent bone.
- "y": The local y position of the bone relative to the parent bone.
- "angle": The local rotation of the bone relative to the parent bone.
- "xscale": The local horizontal scale of the bone, in reference to the parent bone.
- "yscale": The local vertical scale of the bone, in reference to the parent bone.
- "worldX": The x position of the bone relative to the root of the animation (this is a read only value).
- "worldY": The y position of the bone relative to the root of the animation (this is a read only value).
- "worldScaleX": The horizontal scale of the bone in "global" space (ignoring any transforms by GameMaker: Studio), as opposed to the scale with reference to its parent bone (this is a read only value).
- "worldScaleY": The vertical scale of the bone in "global" space (ignoring any transforms by GameMaker: Studio), as opposed to the scale with reference to its parent bone (this is a read only value).
- "worldAngle": The rotation of the bone relative to the root of the animation (this is a read only value).
- "parent": The name (a string) of the parent bone.
As you can see, that's a lot of information! What you need will depend very much on what you want to do, and in our example we simply need the current "worldX" and "worldY" values so that we can correctly
calculate the angle to point at. This is done below, using the ds_map_find_value() functions, and then we do some maths to get the correct angle.
After that we then set the "angle" value for the bone, again using a ds_map function, and the map is used to update the bone data again using the function skeleton_bone_state_set(). We can
then destroy the map again as it is of no further use to us.
Run the game again now, and you'll see that the player figures head will track the mouse, although if you were doing this in a real game you'd probably want to clamp the angle so that it can't twist round backwards!
© Copyright YoYo Games Ltd. 2014 All Rights Reserved