So im trying to wrap my head around this.
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 / Columns; int height = Texture.Height / Rows; 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.Begin(); spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White); spriteBatch.End(); } } }
Can anyone help me break this down. the part where im stuck is if currentFrames = 0, then when you divide by the columns, won't you always get zero? I'm apologize if this is a dumb question. I did try just copypasting the code, and it works, but I am trying to figure out WHY it works.