简体   繁体   English

如何在XNA游戏中进行精灵跳跃?

[英]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. 我是XNA的新手,在尝试使Sprite跳跃时遇到了困难。 So far all the sprite can do is move left and right across the X axis. 到目前为止,所有精灵只能在X轴上左右移动。

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. UpdateSprite() ,您应该检查一下跳转按钮。 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. 然后,您可以检查是否释放了琴键,并将其降低到原始spritePosition.Y ,或者将其降低到最大高度。 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. 但是,您需要确保将当前的spritePosition.Y保存到temp变量中,以便可以恢复到原始状态。

This is by no means EXACTLY how you should do it, but it should be a good start to help you figure it out. 这绝不是您应该怎么做,但它应该是帮助您弄清楚它的一个好的开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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