Rewrote this a few times heres the meat and potatoes. Has a few issues such as flashing when moving in different directions quickly.
Thinking of trying to clamp a few values to try to stop the diagonal movement,flashing and smoothing out the sprite. Something along these lines might update this later.
A Way to animate a sprite
Materials
- 2d_GameSkeleton
- GameStateManagement(not necessary)
- a few minutes.
- Sprite Sheet Packer
Setup your integer values from sprite sheet packer XML
something like this
int leftx
leftx = coordinate value
// make a rectangle
Rectangle mcsr
mcsr.X = coordinate from xml
mcsr.Y coordinate from xml
if (keyboardState.IsKeyDown(Keys.Left))
{
movement.X--;
asprite.SourceRectangle = mcsr;
while (sum >= 6)
{
movement.X--;
asprite.SourceRectangle = new Rectangle(leftx, lefty, width, height);
sum = 1;
if (s == true)
{
//Incremement timer using paramater
m_timer += (float)gameTime.ElapsedGameTime.Milliseconds;
//Check if timer is greater than interval
if (m_timer > m_interval)
{
movement.X--;
asprite.SourceRectangle = new Rectangle(leftx1, lefty1, width, height);
//Reset timer
m_timer = 700f;
if (keyboardState.IsKeyDown(Keys.Space))
{
m_interval = 775;
playerPosition += movement * 2.7f;
if (keyboardState.IsKeyUp(Keys.Space))
{
m_interval = 775f;
playerPosition += movement * 1.7f;
}
}
}
}
}
}