Don't like ads? PRO users don't see any ads ;-)
Guest

Chrille e dum :P

By: a guest on Apr 16th, 2012  |  syntax: C#  |  size: 1.66 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. namespace Valfrittspel
  9. {
  10.     class Animering
  11.     {
  12.         Texture2D textur;
  13.  
  14.         float hastighet = 0.05f;
  15.         float elapsed = 0.0f;
  16.  
  17.         Vector2 Position = new Vector2(0, 0);
  18.         int width = 64, height = 64;
  19.  
  20.         int antalBilder = 16;
  21.         int aktuellFrame = 0;
  22.  
  23.         public int Frame
  24.         {
  25.             get { return aktuellFrame; }
  26.             set { aktuellFrame = (int)MathHelper.Clamp(value, 0, antalBilder); }
  27.         }
  28.         public Animering(Texture2D texture, int nyY)
  29.         {
  30.             textur = texture;
  31.             Position.Y = nyY;
  32.         }
  33.  
  34.         public Rectangle GetSourceRect()
  35.         {
  36.             return new Rectangle(
  37.             (int)Position.X + (width * aktuellFrame),
  38.             (int)Position.Y,
  39.             width,
  40.             height);
  41.         }
  42.  
  43.         public void Update(GameTime gametime)
  44.         {
  45.             elapsed += (float)gametime.ElapsedGameTime.TotalSeconds;
  46.  
  47.             if (elapsed > hastighet)
  48.             {
  49.                 aktuellFrame = (aktuellFrame + 1) % antalBilder;
  50.  
  51.                 elapsed = 0.0f;
  52.             }
  53.         }
  54.  
  55.         public void Draw(SpriteBatch spriteBatch, Vector2 position)
  56.         {
  57.  
  58.             spriteBatch.Draw(
  59.                 textur,
  60.                 new Rectangle(
  61.                   (int)position.X,
  62.                   (int)position.Y,
  63.                   width,
  64.                   height),
  65.                 GetSourceRect(),
  66.                 Color.White);
  67.         }
  68.     }
  69. }