All pastes #590059 Raw Edit

Animated Sprite

public text v1 · immutable
#590059 ·published 2007-06-26 09:47 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Tiles
{
    class AnimatedSprite
    {
        public AnimatedSprite(int frameCount, GraphicsDeviceManager graphics)
        {
            frames = frameCount;
            Graphics = graphics;
        }

        #region Fields
        private int delayTime = 30;
        private int currentTime = 1;
        private int currentFrame = 0;
        private GraphicsDeviceManager Graphics;
        private Texture2D texture;
        private Color color;
        public List<Texture2D> textures = new List<Texture2D>();
        private Vector2 position;

        private float layer;
        private int frames;
        public bool draw = true;
        public bool loop = false;
        #endregion
        #region Properties
        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        public float Layer
        {
            get { return layer; }
            set { layer = value; }
        }
        #endregion

        public void Draw(SpriteBatch spriteBatch)
        {
            Rectangle sourceRectangle = new Rectangle(0, 0, textures[0].Width, textures[0].Height);
            if (draw)
            {
                if (currentTime == delayTime)
                {
                    Graphics.GraphicsDevice.Clear(Color.Black);
                    spriteBatch.Draw(textures[currentFrame],
                    position,
                    sourceRectangle,
                    color,
                    0.0f,
                    new Vector2(0, 0),
                    1.0f,
                    SpriteEffects.None,
                    layer);
                    if (currentFrame < textures.Count - 1)
                    {
                        currentFrame++;
                    }
                    else
                    {
                        if (!loop)
                        {
                            draw = false;
                            Graphics.GraphicsDevice.Clear(Color.Black);
                        }
                        else
                        {
                            currentFrame = 0;
                        }
                    }
                }
                else
                {
                    currentTime++;
                }
            }
        }
    }
}