PlugaruT
PlugaruT

Reputation: 154

Character jump in XNA

I am new in Game Programming but I'm interested in this domain; now I am working on a small game for my course work. I have used some ideas from the internet to make my hero jump; the code is working, but after I press first time space, the hero is jumping and is not coming back to his position, he remains on the top of the screen. Please help me to make my hero, then return to his initial position. If I press space again he is jumping, but is jumping there, on the top of the screen.

public void Initialize()
{
    startY = position.Y;
    jumping = false;
    jumpspeed = 0;
}

public void Update(GameTime gameTime)
{
    KeyboardState keyState = Keyboard.GetState();
    rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
    origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
    AnimateRight(gameTime);//calling AnimateRight function to animate heroes sprite
    if (jumping)
    {
        position.Y += jumpspeed;
        jumpspeed += 1;
        if (position.Y >= startY)
        {
            position.Y = startY;
            jumping = false;
        }
    }
    else
    {
        if (keyState.IsKeyDown(Keys.Space))
        {
            jumping = true;
            jumpspeed = -14;

        }
    }
}

Upvotes: 0

Views: 569

Answers (2)

Ryan Foy
Ryan Foy

Reputation: 322

I know that you are new, but I replicated a movement system that worked great if you can understand it, here is the link to see the player movement in action, and here is the web site for it. If you want to download it here is the link.

This player movement uses a couple important things,

First:

Inside the Player class there is a method called public Player, yes, you make a method that is the same name as the class. By doing this you can transfer information from the Game1 class to Player class. So you can send the player texture, position, speed, ect...

Second:

Inside the Player method the info that is called over from the Game1 class needs to be collected and stored in the Player class. So if you wanted to transfer the texture of you player you would need to do the following.

Create the Player Texture and create a the link that will allow you to create the link to the Player class:

Texture2D personTexture;

'Player player'

Then in the Load Content you need to call on the personTexture and put it into the player function:

personTexture = Content.Load <Texture2D>("Person");

player = new Player(personTexture);

Now that the Texture is now in side the Player method in the Player class you will now store it in the Player class so that you can use it, add a Texture2D Texture to your Player class then enter the following:

public Player(Texture2D Texture) 
{
     this.Texture = Texture;//this.Texture is the one you create in side the 
     Player class, the other is the Texture you stated 
}

Now your done and able to use your texture in that class.

Hopefully this will help you understand how to create your jumping player.

Upvotes: 0

Nico Schertler
Nico Schertler

Reputation: 32667

You have to set startY when pressing Space:

if (keyState.IsKeyDown(Keys.Space))
{
    jumping = true;
    jumpspeed = -14;
    startY = position.Y;
}

Upvotes: 1

Related Questions