XNA -保持更长时间跳得更高(附例)

本文关键字:附例 长时间 XNA | 更新日期: 2023-09-27 18:07:10

我最近问了一个问题,但仍然不明白,我的意思是答案很好,但我就是不明白。我试图让玩家在按住空格键的时间越长,跳得越高。高度也需要有一个最大值。

我有一个球员类的例子,我正试图添加这样一个功能。如果有人能看一下这个类,添加函数,然后可能添加一些评论来帮助我,我会非常感激。-谢谢。

Player.cs

namespace Jumping

{

class Player
{
    public Texture2D Texture;
    public Vector2 Velocity;
    public Vector2 Position;
    public float ground;
    private float Speed;
    private Rectangle screenBound;
    public bool isJumping; //are we jumping or not
    public bool goingUp; //if going up or not
    public float u; //initial velocity
    private float jumpU;
    private float g; //gravity
    public float t; //time
    private KeyboardState prevKB;
    public Player(Texture2D Texture, Vector2 Position, float Speed, Rectangle screenBound)
    {
        this.Texture = Texture;
        this.Position = Position;
        ground = Position.Y;
        this.Speed = Speed;
        this.screenBound = screenBound;
        Velocity = Vector2.Zero;
        isJumping = goingUp = false;
        jumpU = 2.5f;
        g = -9.8f;
        t = 0;
    }
    public void Update(GameTime gameTime)
    {
        Position.X += (Velocity.X * Speed);
        //Set the Y position to be subtracted so that the upward movement would be done by decreasing the Y value
        Position.Y -= (Velocity.Y * Speed);
        goingUp = (Velocity.Y > 0);
        // TODO: Add your update logic here
        if (isJumping == true)
        {
            //motion equation using velocity: v = u + at
            Velocity.Y = (float)(u + (g * t));
            //Increase the timer
            t += (float)gameTime.ElapsedGameTime.TotalSeconds;
        }
        if (isJumping == true && Position.Y > screenBound.Height - Texture.Height)
        {
            Position.Y = ground = screenBound.Height - Texture.Height;
            Velocity.Y = 0;
            isJumping = false;
            t = 0;
        }
        if (Position.X < 0)
        {
            //if Texture touches left side of the screen, set the position to zero and the velocity to zero.
            Position.X = 0;
            Velocity.X = 0;
        }
        else if (Position.X    + Texture.Width > screenBound.Width)
        {
            //if Texture touches left side of the screen, set the position to zero and the velocity to zero.
            Position.X = screenBound.Width - Texture.Width;
            Velocity.X = 0;
        }
        if (Position.Y < 0)
        {
            //if the Texture touches the top of the screen, reset the timer and set the initial velocity to zero.
            Position.Y = 0;
            t = 0;
            u = 0;
        }
    }
    public void Input(KeyboardState keyState)
    {
        if (keyState.IsKeyDown(Keys.Space) && (isJumping == false || Position.Y == ground))
        {
            isJumping = true;
            u = jumpU;
        }
        if (keyState.IsKeyDown(Keys.Left) && !keyState.IsKeyDown(Keys.Right))
        {
            if (keyState.IsKeyDown(Keys.A))
            {
                if (Velocity.X > -2.0f)
                Velocity.X -= (1.0f / 10);
            }
            else if (Velocity.X > -1.0f)
            {
                Velocity.X -= (1.0f / 10);
            }
            else
            {
                Velocity.X = -1.0f;
            }
        }
        else if (!keyState.IsKeyDown(Keys.Left) && keyState.IsKeyDown(Keys.Right))
        {
            if (keyState.IsKeyDown(Keys.A))
            {
                if (Velocity.X < 2.0f)
                Velocity.X += (1.0f / 10);
            }
            else if (Velocity.X < 1.0f)
            {
                Velocity.X += (1.0f / 10);
            }
            else
            {
                Velocity.X = 1.0f;
            }
        }
        else
        {
            if (Velocity.X > 0.05 || Velocity.X < -0.05)
                Velocity.X *= 0.90f;
            else
                Velocity.X = 0;
        }
        prevKB = keyState;
    }
    public void Fall()
    {
        t = 0;
        u = 0;
    }
    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height), Color.White);
    }
}

}

XNA -保持更长时间跳得更高(附例)

为了帮助您,不让您感到困惑,我将建议一种方法,但它必须在稍后进行重构。

在你的输入法中添加它:

public void Input(KeyboardState keyState)
{
   newIsJumping = keyState.IsKeyDown(Keys.Space);
   ...

在你的Update方法中,使用它:

// TODO: Add your update logic here
if (isJumping == true)
{
   if (newIsJumping) u += 0.5f; // use some constant here
   ...

我希望它能解决你的问题。