Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 can't seem to find the problem. I have also include 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);
        }
    }
}
share|improve this question
5  
This is not the place for people to debug your code for you. Learn to use the debugger, step through your code or set break points. You can figure out what's wrong far more easily than anyone here. –  Byte56 Dec 8 '12 at 16:08
    
Which part of the code do you think isn't working? –  Anko Dec 8 '12 at 16:29
add comment

closed as too localized by Byte56, Trevor Powell, Josh Petrie, Laurent Couvidou, bummzack Dec 16 '12 at 13:19

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

In UpdateJump this seems wrong, doesn’t it?

if (presentKeyboardState.IsKeyDown(Keys.Space)
    && !presentKeyboardState.IsKeyDown(Keys.Space))

Guess you meant to use previousKeyboardState:

if (presentKeyboardState.IsKeyDown(Keys.Space)
    && !previousKeyboardState.IsKeyDown(Keys.Space))
share|improve this answer
    
thanks that worked abit but now when i jump it only goes through 2 of the jump animation frames XD –  muzzy Dec 8 '12 at 14:23
    
I cannot see it directly from code, but I worked on the same (in pygame) recently and it also was a lot of trouble. Maybe you can do a bit of debugging by setting member variables in regions like UpdateJump at if (playerPos.Y > originalPlayerPos.Y) and later you print them to console in Draw(). Then you could see which parts are executed and which not (at least I did not find out how to best debug with XNA, but I am also a Visual Studio noob). Or do you mean it always cycles two animation frames? –  Aufziehvogel Dec 8 '12 at 15:06
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.