Tutorial

Page 6 of 10

Mixing Animation Sets

While testing our tutorial game, you may have noticed that the transition from the "run" animation to the "jump" animation is far from smooth. Depending on when you press <Space>, you will get a stutter between frames as one part of the skeleton instantly moves to another pose when the animation sets are changed. Traditionally, with bitmap sprites, you would have designed your sprites to avoid this and be checking the image_index before transitioning to another sprite. With Spine sprites, things are actually much simpler...

Since skeletal animations are based on key frames with the animation itself being interpolated between them, we can use this to mix two different animations together. This "tweening" is achieved by taking the key frames of one animation and interpolating from them into the key frames of another animation, and GameMaker: Studio provides a function to do just that.

In our oPlayer object, we need to modify the Create Event again, so open up the code editor for that event. We are going to add the following two lines of code:



Here we are telling GameMaker that when we change animation sets they should be mixed together by the given amount. The amount is a value from 0 to 1, with 0.5 being a 50/50 mix between both sets. A lower value will mix using more of the second animation, and a higher value will mix using more of the first animation. The values used here are what we consider optimal for this animation, but play with them and see how changing them affects the mix.

Drawing The Sprite

As mentioned previously, the image_* variables that all instances have are fine to use to control your animations. But you can also use them to control how the final sprite is drawn, irrespective of the animation set or skin that is being used.

Currently our tutorial game lets GameMaker default draw our sprite, which is fine for the tutorial, but in a real game you may want more control. This is not a problem however as we have a function to do just that, and it works in almost the exact same way as draw_sprite_ext does for bitmap sprites. To show this, add a Draw Event to our player object with this code:



We need to first get the current animation and skin, as the function requires these details so that it knows what to draw, and then we change the xscale, yscale, angle, colour and alpha of the sprite. If you run the game now you will see quite a different image to what it was before!
You can also use this function along with the built in instance variables like image_xscale or image_angle, and then change these values elsewhere in the object and they will be used for drawing, just like with a traditional sprite.
NOTE: Changing the image_alpha may not give the expected results! Remember, the "skin" for a skeletal animation is really made up of many component sprites that are assigned to "bones" in the animation. Therefore where they overlap, the alpha values will be different.
You can remove this code now from the tutorial game, as we want to have the sprite drawn as normal since the next section deals with changing skins, and you want to be able to see them correctly!



© Copyright YoYo Games Ltd. 2014 All Rights Reserved