Becky O Brien
Becky O Brien

Reputation: 69

Jump will only happen once

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.

Upvotes: 1

Views: 131

Answers (1)

gleng
gleng

Reputation: 6304

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)

Upvotes: 1

Related Questions