Advertisement
Guest User

AnimatedSprite

a guest
Sep 20th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Graphics;
  7.  
  8.  
  9.  
  10. namespace CPI311.Game_Engine
  11. {
  12.     public class AnimatedSprite : Sprite
  13.     {
  14.         public int FrameWidth;
  15.         public int FrameHeight;
  16.         public int Rows;
  17.         public int Columns;
  18.         public double Speed;
  19.         public double delta = 0;
  20.         public int FrameRow = 0;
  21.         public int FrameColumn = 0;
  22.  
  23.         public AnimatedSprite(Texture2D texture, int frameWidth, int frameHeight, int rows, int columns, double speed) : base(texture)
  24.         {
  25.             FrameWidth = frameWidth;
  26.             FrameHeight = frameHeight;
  27.             Rows = rows;
  28.             Columns = columns;
  29.             Speed = speed;
  30.         }
  31.  
  32.         public void Update(GameTime gameTime)
  33.         {
  34.             delta += gameTime.ElapsedGameTime.Milliseconds;
  35.  
  36.             if(delta >= 1/Speed * 1000)
  37.             {
  38.                 delta = 0;
  39.                 NextFrame();
  40.             }
  41.         }
  42.  
  43.         public void NextFrame()
  44.         {
  45.             if (FrameColumn < Columns-1)
  46.                 FrameColumn += 1;
  47.            
  48.             else
  49.             {
  50.                 if(FrameRow < Rows-1)
  51.                 {
  52.                     FrameColumn = 0;
  53.                     FrameRow += 1;
  54.                 }
  55.                 else
  56.                 {
  57.                     FrameColumn = 0;
  58.                     FrameRow = 0;
  59.                 }
  60.                
  61.             }
  62.         }
  63.  
  64.         public void Draw(SpriteBatch spriteBatch)
  65.         {
  66.             Rectangle source = new Rectangle(FrameColumn*FrameWidth, FrameRow*FrameHeight, FrameWidth, FrameHeight);
  67.  
  68.             spriteBatch.Draw(Texture, Position, source, Color,
  69.                 Rotation, Origin, Scale, Effects, Layer);
  70.         }
  71.  
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement