Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[C#/XNA]Custom Animated Sprite Class

akakori
Joined
Apr 3, 2008
Messages
368
Reaction score
98
This belongs to a custom animated sprite script i made which is based on the original animated sprite class of microsoft.

Code:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AnimatedTextureChart
{
    class AnimatedTextureCustom
    {
        private int rowCount;
        private List<int> framePerRow = new List<int>();
        private Texture2D myTexture;
        private float elapsedTime;
        private float TimePerFrame;
        private int Frame;
        private bool Paused;
        private int Row;

        private float Rotation, Scale, Depth;
        private Vector2 Origin;
        public AnimatedTextureCustom(Vector2 origin, float rotation,
            float scale, float depth)
        {
            this.Origin = origin;
            this.Rotation = rotation;
            this.Scale = scale;
            this.Depth = depth;
        }

        public void Load(ContentManager content, string asset, int frameSpeed)
        {
            TimePerFrame = (float)1/frameSpeed;
            elapsedTime = 0;
            myTexture = content.Load<Texture2D>(asset);
            Paused = false;
            Frame = 0;
        }

        public void defineFrames(int rows, int[] frames)
        {
            for (int i = 0; i <= rows-1; i++)
            {
                framePerRow.Add(frames[i]);
            }
            rowCount = rows;
        }

        public void UpdateFrame(float elapsed)
        {
            if (Paused)
                return;
            elapsedTime += elapsed;

            if (elapsedTime > TimePerFrame)
            {
                    Frame++;
                // Keep the Frame between 0 and the total frames, minus one.
                Frame = Frame % framePerRow[Row];
                elapsedTime -= TimePerFrame;
            }
        }

        public void DrawFrame(SpriteBatch batch, Vector2 screenPos)
        {
            DrawFrame(batch, Row, Frame, screenPos);
        }
        public void DrawFrame(SpriteBatch batch, int row, int frame, Vector2 screenPos)
        {
            int FrameWidth = myTexture.Width / framePerRow[row];
            int FrameHeight = myTexture.Height / rowCount;
            int FrameHeightStart = 0 + FrameHeight * row;
            Rectangle sourcerect = new Rectangle(FrameWidth * frame, FrameHeightStart,
                FrameWidth, FrameHeight);
            batch.Draw(myTexture, screenPos, sourcerect, Color.White,
                Rotation, Origin, Scale, SpriteEffects.None, Depth);
        }

        public void setRow(int newRow)
        {
            Row = newRow-1;
        }

        public void increaseRotation(int degree)
        {
            Rotation += (float)3.14 * degree;
        }

        public void decreaseRotation(int degree)
        {
            Rotation -= (float)3.14 * degree;
        }

        public bool IsPaused
        {
            get { return Paused; }
        }
        public void Reset()
        {
            Frame = 0;
            elapsedTime = 0f;
        }
        public void Stop()
        {
            Pause();
            Reset();
        }
        public void Play()
        {
            Paused = false;
        }
        public void Pause()
        {
            Paused = true;
        }
    }
}

Usage:

define
Code:
private AnimatedTextureCustom animatedTexture;
private Viewport viewPort;
public Vector2 startPos;

add to load
Code:
viewPort = graphics.GraphicsDevice.Viewport;
startPos = new Vector2(viewPort.Width / 2, viewPort.Height / 2);
            animatedTexture = new AnimatedTextureCustom(Vector2.Zero, 0.0f, 1.0f, 0.5f);
            animatedTexture.Load(Content, "phoenix", 6);
            int[] frames = new int[4] { 4, 4, 4, 4 };
            animatedTexture.defineFrames(4, frames);
            animatedTexture.setRow(1);

add to update:
Code:
animatedTexture.UpdateFrame((float)gameTime.ElapsedGameTime.TotalSeconds);

add to draw:
Code:
sBatch.Begin();
            animatedTexture.DrawFrame(sBatch, startPos);
            sBatch.End();

Below is the image i used for this sample... It can load a chart format instead of a strip format(original msdn). The loaded chart does not have to be a 4 by 4..

It can be 5 by 5 with a row with only 3 images.. it will still work. but images have to have equal width within the row.
 

Attachments

You must be registered for see attachments list
akakori
Joined
Apr 3, 2008
Messages
368
Reaction score
98
there's is really anyway to explain this script other than looking it thru yourself...

side note. It is possible to alter this script to process different frame speed for different rows too.. =)
 
Back
Top