What I'm trying to achieve is a realistic walk animation where a character is at a static frame and then when Right is pressed it will load the character animation I created.
I've tried messing around with this and so far all I've gotten are errors.
Here is my code:
Sprite
public class Sprite
{
public static SpriteManager spriteManager;
private int sectorNumber;
private string name;
private TextureData textureData;
private SpritePresentationInfo spritePresentationInfo;
private SpritePositionInfo spritePositionInfo;
public Sprite(string name, TextureData textureData,
SpritePresentationInfo spritePresentationInfo,
SpritePositionInfo spritePositionInfo)
{
this.name = name;
this.textureData = textureData;
this.spritePresentationInfo = spritePresentationInfo;
this.spritePositionInfo = spritePositionInfo;
//make sure we set first time around
this.sectorNumber = Collision.getSectorNumber(this.POSITIONINFO.BOUNDS);
}
AnimatedSprite
public class AnimatedSprite : Sprite
{
protected AnimatedTextureData animatedTextureData;
protected int frameRate, startFrame, currentFrame = 0;
protected bool bRepeatAnimation, bPause;
protected double timeSinceLastFrameInMs, timeBetweenFrameInMs;
#region PROPERTIES
#endregion
public AnimatedSprite(string name, AnimatedTextureData animatedTextureData,
SpritePresentationInfo spritePresentationInfo,
SpritePositionInfo spritePositionInfo,
int frameRate, int startFrame, bool bRepeatAnimation)
: base(name, animatedTextureData, spritePresentationInfo, spritePositionInfo)
{
this.animatedTextureData = animatedTextureData;// (AnimatedTextureData)animatedTextureData.Clone();
this.frameRate = frameRate;
timeBetweenFrameInMs = 1000.0 / frameRate; //time between each frame if they play at frameRate per second (e.g. 24fps gives timeBetweenFrameInMs = 1000ms/24 per second)
timeSinceLastFrameInMs = timeBetweenFrameInMs; //means no initial delay in animation
this.startFrame = startFrame;
currentFrame = startFrame;
this.bRepeatAnimation = bRepeatAnimation;
}
PlayerSprite.
public class PlayerSprite : Sprite
{
protected Keys leftKey, rightKey;
//private float moveIncrement = 0.5f;
private int move;
public PlayerSprite(string name, AnimatedTextureData textureData, SpritePresentationInfo spritePresentationInfo,
SpritePositionInfo spritePositionInfo, Keys leftKey, Keys rightKey)
: base(name, textureData, spritePresentationInfo, spritePositionInfo)
{
this.leftKey = leftKey;
this.rightKey = rightKey;
}
//-----------------------------------------------------------------------------------
// Use this if we do not want to use the parents
//-----------------------------------------------------------------------------------
public override void Update(GameTime gameTime)
{
handleInput(gameTime);
base.Update(gameTime);
}
//-----------------------------------------------------------------------------------
// Use this if we do not want to use the parents
//-----------------------------------------------------------------------------------
protected override void handleInput(GameTime gameTime)
{
this.move = (int)(GameData.PLAYER_MOVE_INCREMENT * gameTime.ElapsedGameTime.Milliseconds);
if (SpriteManager.GAME.KEYBOARDMANAGER.isKeyDown(leftKey))
MoveBy(-move, 0);
//This is where i want to initialize the walk animation.
if (SpriteManager.GAME.KEYBOARDMANAGER.isKeyDown(rightKey))
MoveBy(move, 0);
}
Main. I have it working and animating without a key press in main.
textureManager.Add("PlayerAnimation", "Assets\\Animations\\Characters\\Player\\Player_AnimationFinal", 8, 75,200);
//------------------------------------------------------------------------------------------------
//Player Animation
//------------------------------------------------------------------------------------------------
AnimatedTextureData playerAnimatedTextureData = (AnimatedTextureData)textureManager.Get("PlayerAnimation");
SpritePresentationInfo playerAnimatedPresentationInfo = new SpritePresentationInfo(playerAnimatedTextureData.FULLSOURCERECTANGLE, 0);
SpritePositionInfo playerAnimatedPositionInfo = new SpritePositionInfo(new Vector2(100, 700), playerAnimatedTextureData.Width(), playerAnimatedTextureData.Height(), 0, 2, playerAnimatedTextureData.CENTREORIGIN);
spriteManager.Add(new AnimatedSprite("PlayerAnimation", playerAnimatedTextureData, playerAnimatedPresentationInfo, playerAnimatedPositionInfo,10, 0, true));
//------------------------------------------------------------------------------------------------
Best way is to use sprite sheet for animations. one single image that contain all sprites. And then simple by changing currentFrame
(from 0 to 7) you show only this part of image.
sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight)
and draw like this
SpriteBatch.Draw (Texture2D, Position, sourceRect, Color)
example of sprite sheet image