Character jump animation is not working when i hit the space bar
Posted
by
muzzy
on Game Development
See other posts from Game Development
or by muzzy
Published on 2012-12-08T13:00:22Z
Indexed on
2012/12/08
17:22 UTC
Read the original article
Hit count: 285
i am having an issue with my game in XNA. My jump sprite sheet for my character does not trigger when i hit the space bar. I cant seem to find the problem. Please help me. I am also put the code below to make things easier.
namespace WindowsGame4 {
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
// start of new code
Texture2D playerWalk; // sprite sheet of walk cycle (14 frames)
Texture2D idle; // idle animation
Texture2D jump; // jump animation
Vector2 playerPos; // to hold x and y position info for the player
Point frameDimensions; // to hold width and height values for the frames
int presentFrame; // to record which frame we are on at any given time
int noOfFrames; // to hold the total number of frames in the spritesheet
int elapsedTime; // to know how long each frame has been shown
int frameDuration; // to hold info about how long each frame should be shown
SpriteEffects flipDirection; // SpriteEffects object
int speed; //rate of movement
int upMovement;
int downMovement;
int rightMovement;
int leftMovement;
int jumpApex;
string state; //this is going to be "idle","walking" or "jumping".
KeyboardState previousKeyboardState;
Vector2 originalPlayerPos;
Vector2 movementDirection;
Vector2 movementSpeed;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
// textures will be defined in the LoadContent() method
playerPos = new Vector2(0, 200); // starting position for the player is at the left of the screen, and a Y position of 200
frameDimensions = new Point(55, 65); // each frame in the idle sprite sheet is 55 wide by 65 high
presentFrame = 0; // start at frame 0
noOfFrames = 5; // there are 5 frames in the idle cycle
elapsedTime = 0; // set elapsed time to start at 0
frameDuration = 80; // 80 milliseconds is how long each frame will show for (the higher the number, the slower the animation)
flipDirection = SpriteEffects.None; // set the value of flipDirection to none
speed = 200;
upMovement = -2;
downMovement = 2;
rightMovement = 1;
leftMovement = -1;
jumpApex = 100;
state = "idle";
previousKeyboardState = Keyboard.GetState();
originalPlayerPos = Vector2.Zero;
movementDirection = Vector2.Zero;
movementSpeed = Vector2.Zero;
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
playerWalk = Content.Load<Texture2D>("sprites/walkSmall"); // load the walk cycle spritesheet
idle = Content.Load<Texture2D>("sprites/idleCycle"); // load the idle cycle sprite sheet
jump = Content.Load<Texture2D>("sprites/jump"); // load the jump cycle sprite sheet
}
protected override void UnloadContent() // we're not using this method at the moment
{
}
protected override void Update(GameTime gameTime) // Update method - used it to call a number of other methods
{
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
{
this.Exit(); // Exit the game if the Escape key is pressed
}
KeyboardState presentKeyboardState = Keyboard.GetState();
UpdateMovement(presentKeyboardState, gameTime);
UpdateIdle(presentKeyboardState, gameTime);
UpdateJump(presentKeyboardState);
UpdateAnimation(gameTime);
playerPos += movementDirection * movementSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
previousKeyboardState = presentKeyboardState;
base.Update(gameTime);
}
private void UpdateAnimation(GameTime gameTime)
{
elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
if (elapsedTime > frameDuration)
{
elapsedTime -= frameDuration;
elapsedTime = elapsedTime - frameDuration;
presentFrame++;
if (presentFrame > noOfFrames)
if (state != "jumping")
{
presentFrame = 0;
}
else
{
presentFrame = 8;
}
}
}
protected void UpdateMovement(KeyboardState presentKeyboardState, GameTime gameTime)
{
if (state == "idle")
{
movementSpeed = Vector2.Zero;
movementDirection = Vector2.Zero;
if (presentKeyboardState.IsKeyDown(Keys.Left))
{
state = "walking";
movementSpeed.X = speed;
movementDirection.X = leftMovement;
flipDirection = SpriteEffects.FlipHorizontally;
}
if (presentKeyboardState.IsKeyDown(Keys.Right))
{
state = "walking";
movementSpeed.X = speed;
movementDirection.X = rightMovement;
flipDirection = SpriteEffects.None;
}
}
}
private void UpdateIdle(KeyboardState presentKeyboardState, GameTime gameTime)
{
if ((presentKeyboardState.IsKeyUp(Keys.Left) && previousKeyboardState.IsKeyDown(Keys.Left)
|| presentKeyboardState.IsKeyUp(Keys.Right) && previousKeyboardState.IsKeyDown(Keys.Right)
&& state != "jumping"))
{
state = "idle";
}
}
private void UpdateJump(KeyboardState presentKeyboardState)
{
if (state == "walking" || state == "idle")
{
if (presentKeyboardState.IsKeyDown(Keys.Space) && !presentKeyboardState.IsKeyDown(Keys.Space))
{
presentFrame = 1;
DoJump();
}
}
if (state == "jumping")
{
if (originalPlayerPos.Y - playerPos.Y > jumpApex)
{
movementDirection.Y = downMovement;
}
if (playerPos.Y > originalPlayerPos.Y)
{
playerPos.Y = originalPlayerPos.Y;
state = "idle";
movementDirection = Vector2.Zero;
}
}
}
private void DoJump()
{
if (state != "jumping")
{
state = "jumping";
originalPlayerPos = playerPos;
movementDirection.Y = upMovement;
movementSpeed = new Vector2(speed, speed);
}
}
protected override void Draw(GameTime gameTime) // Draw method
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(); // begin the spritebatch
if (state == "walking")
{
noOfFrames = 14;
frameDimensions = new Point(55, 65);
Vector2 playerWalkPos = new Vector2(playerPos.X, playerPos.Y - 28);
spriteBatch.Draw(playerWalk, playerWalkPos, new Rectangle((presentFrame * frameDimensions.X), 0, frameDimensions.X, frameDimensions.Y), Color.White, 0, Vector2.Zero, 1, flipDirection, 0);
}
if (state == "idle")
{
noOfFrames = 5;
frameDimensions = new Point(55, 65);
Vector2 idlePos = new Vector2(playerPos.X, playerPos.Y - 28);
spriteBatch.Draw(idle, idlePos, new Rectangle((presentFrame * frameDimensions.X), 0, frameDimensions.X, frameDimensions.Y), Color.White, 0, Vector2.Zero, 1, flipDirection, 0);
}
if (state == "jumping")
{
noOfFrames = 9;
frameDimensions = new Point(55, 92);
Vector2 jumpPos = new Vector2(playerPos.X, playerPos.Y - 28);
spriteBatch.Draw(jump, jumpPos, new Rectangle((presentFrame * frameDimensions.X), 0, frameDimensions.X, frameDimensions.Y), Color.White, 0, Vector2.Zero, 1, flipDirection, 0);
}
spriteBatch.End(); // end the spritebatch commands
base.Draw(gameTime);
}
}
}
© Game Development or respective owner