Time for action – building the GameObject class – part 2

  1. Add the updateAnimation() helper method to the GameObject class:
    #region Helper Methods
    private void updateAnimation(GameTime gameTime)
    {
        if (animations.ContainsKey(currentAnimation))
        {
            if (animations[currentAnimation].FinishedPlaying)
            {
               PlayAnimation(animations[currentAnimation].NextAnimation);
            }
            else
            {
               animations[currentAnimation].Update(gameTime);
            }
        }
    }
    #endregion
  2. Add a new region called "Public Methods" to the GameObject class:
    #region Public Methods
    #endregion
  3. Inside the Public Methods region, add the PlayAnimation() method:
    public void PlayAnimation(string name)
    {
        if (!(name==null) && animations.ContainsKey(name))
        {
            currentAnimation = name;
            animations[name].Play();
        }
    }
  4. Still in the Public ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.