AnimationChains are a common object used in Glue. AnimationChains are simply a series of images which can be flipped between to create texture-flipping animation. AnimationChains can be created from multiple, individual images, or they can be created from what is called a “sprite sheet” which is an image containing multiple frames of an animation.
Typically AnimationChains are created from a .achx file.
When are AnimationChains used?
There are two common uses for AnimationChains. The first is to play an animation, such as a character walking. For example, if you were making a platformer, you may create the following AnimationChains:
The second is to define a visual state for Entities. For example, if you were making a button, you could define the following AnimationChains:
When considering the ability to control visible states, keep in mind that an AnimationChain does not have to contain multiple “frames” that it flips between. An AnimationChain can be as simple as one frame, so if you are creating button states, each AnimationChain can be as simple as a single image.
Creating a Button Entity
To show how AnimationChains work, we’ll create a Button Entity. To do this:
- Right-click on your Entities item
- Select Add Entity
- Name your Entity Button and press OK
Adding an AnimationChain
Next we’ll add an AnimationChain List File (.achx). The file is a list of AnimationChains because it can contain multiple AnimationChains. To add an AnimationChain List File:
- Right-click on the Files item
- Select “Add File”->”New File”
- Select “Animation Chain List (.achx)” as the type
- Click OK
We’ll create three AnimationChains in this step, but first you’ll need to download the files we’ll use. These are:
Once you’ve downloaded these files, double-click your AnimationChainFile under your Button Entity’s Files item and the AnimationEditor should open. For information on using the AnimationEditor, see the AnimationEditor Tutorial.
Once the AnimationEditor is open, create three AnimationChains – each one referencing one of the images you downloaded
Notice that each AnimationChain has just one image representing the visual state of the button. Once you are finished creating your three AnimationChains, save your AnimationChain and be sure to copy all files.
Creating a Sprite Object
Now that we’ve defined all of our animations in a .achx file, we’ll create a Sprite that can display these animations. To do this:
- Right-click on the Button Entity’s Objects item
- Select Add Object
- Select FlatRedBall or Custom Type
- Select Sprite from the list
- Click OK
Now your Button has a Sprite object. To associate the .achx file you created with the Sprite:
- Select the Sprite
- Make sure the Variables tab is selected
- Change the Animation Chains value to AnimationListChainFile
- Change the Current Chain Name to Normal. This sets the default animation for the Sprite.
Note that the AnimationChainListFile must be in the same entity as the Sprite – otherwise AnimaitonChainListFile will not appear in the drop down.
You can change the Current Chain Name to any value to give the Sprite a default value.
Tunneling the Current Chain Name Variable
Tunneling is useful for setting the SpriteInstance’s CurrentChainName in the following situations:
- Instances of the entity in a Screen or another Entity
- Responding to events when the variable changes
To tunnel the variable:
- Right-click on your Button’s Variables item
- Select “Add Variable”
- Click on the “Tunneling” tab
- Select the SpriteInstance as the Object
- Select the CurrentChain name as Variable
- For brevity, change the “Alternative Name:” to “CurrentChain”
- Click OK
Now you can change the variable by selecting the Sprite in Glue by selecting the Variables folder.
If you are interested in changing the current AnimationChain on a Sprite in code, see this article.
WHEW, all done!
If you’ve read this far, congratulations on sticking through a long tutorial. At this point, the button visible states can be set in code and in Glue, making your Button Entity incredibly flexible. Keep AnimationChains in mind, you can use them for a lot of different things!