繁体   English   中英

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

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

我是XNA的新手,在尝试使Sprite跳跃时遇到了困难。 到目前为止,所有精灵只能在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);
    }
 }

请不要太技术性,我会自学所有我知道的编程知识,所以很多术语将困扰我。 我最清楚地看到在原始代码中实现了答案。 非常感谢,马修。

UpdateSprite() ,您应该检查一下跳转按钮。 就像是:

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

然后,您可以检查是否释放了琴键,并将其降低到原始spritePosition.Y ,或者将其降低到最大高度。 但是,您需要确保将当前的spritePosition.Y保存到temp变量中,以便可以恢复到原始状态。

这绝不是您应该怎么做,但它应该是帮助您弄清楚它的一个好的开始。

暂无
暂无

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

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