Becky O Brien
Becky O Brien

Reputation: 69

Character doesn't jump in XNA

I've been working on character movement and I created a method to make the character jump when the up key is pressed but there is no response in the character's movement.
Can anyone see what is wrong? Here is my code:

Constructor

public PlayerSprite(string name, string magicTextureName,Vector2 position, Vector2 velocity, AnimatedTextureData textureData, SpritePresentationInfo spritePresentationInfo,SpritePositionInfo spritePositionInfo, Keys leftKey, Keys rightKey, int frameRate, int startFrame, bool bRepeatAnimation)
  : base(name, textureData, spritePresentationInfo, spritePositionInfo, frameRate, startFrame, bRepeatAnimation)

Takes in position and velocity.

This is in the update.

if (SpriteManager.GAME.KEYBOARDMANAGER.isFirstKeyPress(Keys.Up))
{
    bPause = false;
    updateJump();
}

updateJump function

 public void updateJump()
 {
     jumpPosition += jumpVelocity;
     if (Keyboard.GetState().IsKeyDown(Keys.Right))
         jumpVelocity.X = 3f;
     else if (Keyboard.GetState().IsKeyDown(Keys.Left))
         jumpVelocity.X = -3f;
     else
         jumpVelocity.X = 0f;

     if (Keyboard.GetState().IsKeyDown(Keys.Up))
     {
         jumpPosition.Y -= 10f;
         jumpVelocity.Y -= 5f;
         hasJumped = true;
     }
     if (hasJumped == true)
     {
         float i = 1;
         jumpVelocity.Y += 0.15f * i;
     }
     if (jumpPosition.Y + animatedTextureData.Height() >= 450)
     {
         hasJumped = false;
     }
     if (hasJumped == false)
     {
         jumpVelocity.Y = 0f;
     }
 }

Where I call this in main

 AnimatedTextureData playerAnimatedTextureData = (AnimatedTextureData)textureManager.Get("PlayerAnimation");
 SpritePresentationInfo playerAnimatedPresentationInfo = new SpritePresentationInfo(playerAnimatedTextureData.FULLSOURCERECTANGLE, 1);
 SpritePositionInfo playerAnimatedPositionInfo = new SpritePositionInfo(new Vector2(100, 700), playerAnimatedTextureData.Width(), playerAnimatedTextureData.Height(), 0, 2, playerAnimatedTextureData.CENTREORIGIN);

 this.playerSprite = new PlayerSprite("PlayerAnimation", "FireballAnimation", new Vector2(50, 50), new Vector2(50, 50), playerAnimatedTextureData, playerAnimatedPresentationInfo, playerAnimatedPositionInfo,Keys.Left, Keys.Right, 10, 0, true);
 spriteManager.Add(playerSprite);

Upvotes: 0

Views: 72

Answers (1)

Petr Hudeček
Petr Hudeček

Reputation: 1763

My guess: I don't know what this function is programmed to do, but if it only detects the pressing down of the Up key, then the Jump is no longer updated after the first frame.

if (SpriteManager.GAME.KEYBOARDMANAGER.isFirstKeyPress(Keys.Up))
{
    bPause = false;
    updateJump();
}

Once you correct this and the updateJump() function is called every Update cycle, then the fireball should escape the screen pretty quickly as long as you hold the Up key.

Upvotes: 3

Related Questions