Ive recently done a class which i can recreate to animate however and howmany sprites as i like.
Problem is , the width and height seem to be getting horrible wrong and are never aligned properly!
Animated class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace FirstGame
{
public class AnimatedSprite
{
public Texture2D Texture { get; set; }
public int Rows { get; set; }
public int Columns { get; set; }
private int currentFrame;
private int totalFrames;
public AnimatedSprite(Texture2D texture, int rows, int columns)
{
Texture = texture;
Rows = rows;
Columns = columns;
currentFrame = 0;
totalFrames = Rows * Columns;
}
public void Update()
{
currentFrame++;
if (currentFrame == totalFrames)
currentFrame = 0;
}
public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
int width = Texture.Width / Rows;
int height = Texture.Height / Columns;
int row = (int)((float)currentFrame / (float)Columns);
int column = currentFrame % Columns;
Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);
spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
}
}}
animatedBullet = new AnimatedSprite(Bullet, 1, 10);
animatedShield = new AnimatedSprite(Shield, 2, 8);
animatedBullet.Update();
animatedShield.Update();
animatedBullet.Draw(spriteBatch, FixOrigin(SpritePosition));
animatedShield.Draw(spriteBatch, shieldOffset);
This is used in my main Game.cs
As you can see the shield is cut off at the top and no matter what kind of numbers i put in , it never comes out right! :@
Uploaded with ImageShack.us