
Chrille e dum :P
By: a guest on
Apr 16th, 2012 | syntax:
C# | size: 1.66 KB | hits: 16 | expires: Never
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Valfrittspel
{
class Animering
{
Texture2D textur;
float hastighet = 0.05f;
float elapsed = 0.0f;
Vector2 Position = new Vector2(0, 0);
int width = 64, height = 64;
int antalBilder = 16;
int aktuellFrame = 0;
public int Frame
{
get { return aktuellFrame; }
set { aktuellFrame = (int)MathHelper.Clamp(value, 0, antalBilder); }
}
public Animering(Texture2D texture, int nyY)
{
textur = texture;
Position.Y = nyY;
}
public Rectangle GetSourceRect()
{
return new Rectangle(
(int)Position.X + (width * aktuellFrame),
(int)Position.Y,
width,
height);
}
public void Update(GameTime gametime)
{
elapsed += (float)gametime.ElapsedGameTime.TotalSeconds;
if (elapsed > hastighet)
{
aktuellFrame = (aktuellFrame + 1) % antalBilder;
elapsed = 0.0f;
}
}
public void Draw(SpriteBatch spriteBatch, Vector2 position)
{
spriteBatch.Draw(
textur,
new Rectangle(
(int)position.X,
(int)position.Y,
width,
height),
GetSourceRect(),
Color.White);
}
}
}