This is the character that we will animate.

And this is the source code :
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace WindowsGame6
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D texture;
Point frameSize = new Point(32, 32);
Point currentFrame = new Point(0, 0);
Point sheetSize = new Point(3,4 );
int timeSinceLastFrame = 0;
int millisecondsPerFrame = 60;
Vector2 pos1 = Vector2.Zero;
float speed1 = 3f;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 60);
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
GraphicsDevice device;
graphics.PreferredBackBufferWidth = 500;
graphics.PreferredBackBufferHeight = 192;
graphics.IsFullScreen = false;
graphics.ApplyChanges();
window.Title = "Animate Character Sprite";
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texture = Content.Load<Texture2D>("man");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
KeyboardState keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.Up))
{
pos1.Y -= speed1;
currentFrame.Y = 0;
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
{
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
}
else if (keyboardState.IsKeyDown(Keys.Down))
{
pos1.Y += speed1;
currentFrame.Y = +2;
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
{
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
}
else if (keyboardState.IsKeyDown(Keys.Right))
{
pos1.X += speed1;
currentFrame.Y = +1;
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
{
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
}
else if (keyboardState.IsKeyDown(Keys.Left))
{
pos1.X -= speed1;
currentFrame.Y = +3;
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame)
{
timeSinceLastFrame -= millisecondsPerFrame;
if (currentFrame.Y >= sheetSize.Y)
currentFrame.Y = 0;
{
++currentFrame.X;
if (currentFrame.X >= sheetSize.X)
{
currentFrame.X = 0;
}
}
}
}
if (pos1.X < 0)
pos1.X = 0;
if (pos1.Y < 0)
pos1.Y = 0;
if (pos1.X > window.ClientBounds.Width - frameSize.X)
pos1.X = window.ClientBounds.Width - frameSize.X;
if (pos1.Y > window.ClientBounds.Height - frameSize.Y)
pos1.Y = window.ClientBounds.Height - frameSize.Y;
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
SpriteSortMode.FrontToBack, SaveStateMode.None);
spriteBatch.Draw(texture, pos1,
new Rectangle(currentFrame.X * frameSize.X,
currentFrame.Y * frameSize.Y,
frameSize.X,
frameSize.Y),
Color.White
, 0, Vector2.Zero, 1, SpriteEffects.None, 0);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
This might not be the best way to do it and i don't have any clues how to do it with a different way.At least it works for me. When i put a value in the currentFrame.Y it means that i want when i press that specific button it will go to that specific collumn of the sprite sheet. In this example i have 4 collumns and three frames for each collumn that referes to the Point sheetSize = new Point(3,4 );
When i have time i will comment it.
Hope it works for you too guys.
And a big Thanx to SixOfEleven for the GIMB tool suggestion now i can make transparent all my sprites.
This post has been edited by Hellbroth: 27 October 2009 - 06:07 AM

New Topic/Question
Reply



MultiQuote





|