简体   繁体   中英

How do I make a sprite jump in my XNA game?

I'm new to XNA and have been having difficulties trying to make my sprite jump. So far all the sprite can do is move left and right across the X axis.

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D sprite;
    Vector2 spritePosition;

    protected override void Update(GameTime gameTime)//
    {
        UpdateSprite(gameTime);
    }

    public Game1()//
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void LoadContent() //
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        sprite = Content.Load<Texture2D>("Main");

    }

    void UpdateSprite(GameTime gameTime) //
    {
        //Sprite Movement and Controlls

        //Movement Perametres, not yet in use

                int MaxX =
                   graphics.GraphicsDevice.Viewport.Width - sprite.Width;
                int MinX = 0;

                int MaxY =
                    graphics.GraphicsDevice.Viewport.Height - sprite.Height;
                int MinY = 0;

                KeyboardState keystate = Keyboard.GetState();


                //Movement Right
                if (keystate.IsKeyDown(Keys.D))
                {
                    sprite = Content.Load<Texture2D>("1");
                }

                if (keystate.IsKeyDown(Keys.D))
                {
                    spritePosition.X = spritePosition.X + 1;

                }

                //Movement Left
                if (keystate.IsKeyDown(Keys.A))
                {
                    spritePosition.X = spritePosition.X - 1;
                }

                if (keystate.IsKeyDown(Keys.A))
                {
                    sprite = Content.Load<Texture2D>("a");
                }

               // Null Movement sprites
                 if (keystate.IsKeyUp(Keys.D)) 
                     if (keystate.IsKeyUp(Keys.A))
                 {
                    sprite = Content.Load<Texture2D>("Main");
                 }

        UpdateSprite(gameTime);
    }

    protected override void Draw(GameTime gameTime)//
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        // TODO: Add your drawing code here

        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        spriteBatch.Draw(sprite, spritePosition, Color.White);
        spriteBatch.End();

        base.Draw(gameTime);
    }
 }

Please don't be too technical, I taught myself all the programming I know so a lot of terminology will be over my head. I best understand answers seeing them implemented in the original code. Many thanks, Matthew.

In your UpdateSprite() , you should have something that checks for your jump button. Something like:

if(keystate.IsKeyDown(Keys.Space))
{
     spritePosition.Y -= 1;
}

Then you can check if the key is released, and lower the position down to the original spritePosition.Y , or once it reaches a max height. But you'll want to be sure to save your current spritePosition.Y into a temp variable, so that you can go back to your original.

This is by no means EXACTLY how you should do it, but it should be a good start to help you figure it out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM