首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在我的XNA游戏中进行精灵跳跃?

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

Stack Overflow用户
提问于 2012-11-10 01:27:53
回答 1查看 900关注 0票数 0

我是XNA的新手,在尝试让我的精灵跳跃时遇到了困难。到目前为止,精灵所能做的就是在X轴上左右移动。

代码语言:javascript
运行
复制
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);
    }
 }

请不要太专业,我自学了所有我知道的编程,所以很多术语都会超出我的理解。通过在原始代码中实现它们,我最能理解答案。非常感谢,马修。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-10 01:35:48

在你的UpdateSprite()中,你应该有一些东西来检查你的跳转按钮。类似于:

代码语言:javascript
运行
复制
if(keystate.IsKeyDown(Keys.Space))
{
     spritePosition.Y -= 1;
}

然后,您可以检查是否释放了键,并将位置降低到原始spritePosition.Y,或者当它达到最大高度时。但是您需要确保将当前的spritePosition.Y保存到一个temp变量中,这样就可以返回到原来的状态。

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13312969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档