1

Hey I'm doing a game in XNA and the game is based on the players ability to jump. I'm having a lot of problems with this, but the one most troublesome is that when I jump I can only do it once this is because it will only happen when hasJumped = false but I can't figure out where to put it. I have a floor in and I've tried to do a function that says if the player is touching the floor set it to false but I can't get that working. hasJumped is defaulted false in the constructor.

Can anyone help me please?

this.Transform.MoveIncrement = Vector2.Zero;
float timeBetweenUpdates = 0.25f * gameTime.ElapsedGameTime.Milliseconds;

if (game.KeyboardManager.isKeyDown(Keys.Left))
{
   this.Transform.MoveIncrement += -this.Transform.Look * timeBetweenUpdates;
   this.Transform.IsMoved = true;
}
if (game.KeyboardManager.isKeyDown(Keys.Right))
{
   this.Transform.MoveIncrement += this.Transform.Look * timeBetweenUpdates;
   this.Transform.IsMoved = true;
}
if (game.KeyboardManager.isKeyDown(Keys.Up) && hasJumped == false)
{
   this.Transform.moveBy(-Vector2.UnitY * 400);
   this.Transform.IsMoved = true;
   hasJumped = true;
}
if (hasJumped == true)
{
   this.Transform.MoveIncrement = Vector2.UnitY * timeBetweenUpdates;
   this.Transform.IsMoved = true;
   // hasJumped = false;
}
if (hasJumped == false)
{
   // Transform.MoveIncrement = new Vector2(0, 0);
   // Transform.IsMoved = false;
}

I added this to my Collision Class to solve this problem.

 PlayerSprite pSprite = (PlayerSprite)collider;
            if((collidee is WallSprite) && (collider is PlayerSprite ))
            {
                pSprite.hasJumped = false;
            }

Collidee and Collider are Sprites this is how i cast it to a PlayerSprite.

2
  • You need to check for collisions, when your player collides with the floor you need to set the hasJumped boolean to back to false. A pretty simple box collision between your character and a surface should tell you. You'll want to make sure that the box coincides with your sprites feet otherwise a collision with a platform may reset the boolean and allow for a double jump when the player shouldn't be able to yet. Dec 19, 2013 at 18:37
  • 1
    gameprogrammingpatterns.com/state.html <-- a particularly good example of this using the state pattern. Dec 19, 2013 at 18:38

1 Answer 1

1

As @Mike McMahon has suggested, you need to do this in your collision section.

You didn't paste any of that code here, but you need to check whether the bounding rectangle of your player intersects with one of your ground tiles. If so, you need to set your hasJumped equal to false, since he is no longer jumping.

There is a good sample called Platformer where you can see how this behavior is done. I'd recommend you downloading it. (Link here)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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