Created: January 2011
Last Updated: March 2011
Original Author: Tony Huynh
Tutorial(s)
Remark: For more on using animations see the official tutorial at http://www.flatredball.com/frb/docs/index.php?title=FlatRedBall.Graphics.Animation.IAnimationChainAnimatable.
Changing Animation Chains
To set the animation chain, set the Sprite.CurrentChainName property.
Detecting When an Animation Chain Completes
To determine when a full loop/cycle of an animation chain is complete, check the Sprite.JustCycled property. Be sure to set the current frame index of the animation chain to 0 via the Sprite.CurrentFrameIndex property each time the animation chain is set in order for the detection of the full animation cycle to work correctly.
Best Practice Example
Because the management of animations can get rather messy when the user needs to determine when an animation loop has completed, we recommend that the user create a custom animation controller class to deal with the management. See an example of one below. Remark (on the code block below): Note that the Update() method does not take in a time parameter; this is because FlatRedBall manages the updating of the animations internally (via the SpriteManager class). Also note that the automatically generated "using" statements from code generated by Glue are not shown.
using FlatRedBall.Graphics;
using System.Diagnostics;
public interface IAnimationController
{
bool LoopEnabled {get;}
bool HasFinished {get;}
void Update();
void SetDefaultAnimationChain(String animationChainName);
void SetAnimationChain(String animationChainName, bool loopEnabled);
}
public class AnimationController : IAnimationController
{
private Sprite mSprite;
private string mDefaultAnimation; // animation chain to be played when none is specified
public bool LoopEnabled {get; private set;}
public bool HasFinished {get; private set;}
/// <summary>
/// Constructor
/// </summary>
/// <param name="sprite">cannot be null</param>
/// <param name="defaultAnimationChainName">animation to be played when none is specified; cannot be null</param>
public AnimationController(Sprite sprite, string defaultAnimationChainName)
{
Debug.Assert(sprite != null, "The sprite parameter cannot be null.");
mSprite = sprite;
Debug.Assert(defaultAnimationChainName != null, "The default animation chain name parameter cannot be null.");
mDefaultAnimation = defaultAnimationChainName;
mSprite.CurrentChainName = mDefaultAnimation;
LoopEnabled = true;
HasFinished = false;
}
public void SetAnimationChain(String animationChainName, bool loopEnabled)
{
mSprite.CurrentChainName = animationChainName;
LoopEnabled = loopEnabled;
HasFinished = false;
mSprite.CurrentFrameIndex = 0; // reset animation frame
}
public void SetDefaultAnimationChain(String animationChainName)
{
Debug.Assert(animationChainName != null, "The idle animation chain parameter cannot be null.");
mDefaultAnimation = animationChainName;
}
public void Update()
{
if (mSprite.JustCycled && !HasFinished && !LoopEnabled)
{
HasFinished = true;
mSprite.CurrentChainName = mDefaultAnimation;
}
}
}