XNA - 保持更长时间跳得更高

时间:2013-04-02 21:07:04

标签: c# xna

我正在为我的游戏寻找一种简单的方法,这样当你按住空格键时,你会跳得更高。当你“点击”时,你不会跳到最大高度。需要有一个最大值,但是,我不知道如何编程。

Anyhelp,非常感谢。将给出反馈。谢谢。

2 个答案:

答案 0 :(得分:3)

在处理跳跃的更新功能中,您可以让它跟踪角色离开地面的时间长度并让它在一段时间后停止向上的速度。

例如,以下内容应该可以正常工作

class Player
{
    private const int gravity = 2; //force of gravity on the object
    private const int maxHeight = 5; //change as needed
    private const int msecsToMax = 1500; //change as needed

    private int yPos = 0;
    private int timeOffGround = 0;
    private yVel = 0;

    //other functions and stuff

    void update(GameTime gt)
    {
        //check if the player is on the ground and update as necessary
        if(isOnGround())
            timeOffGround = 0;
        else
            timeOffGround += gt.ElapsedGameTime.TotalMilliseconds;

        //update the player's velocity
        yVel -= isOnGround() ? 0 : gravity; //no velocity when on the ground
        if(Keyboard.GetState().IsKeyDown(Keys.SPACE) && timeOffGround < msecsToMax))
            yVel += (maxHeight / msecToMax);

        yPos += yVel;
    }
}

答案 1 :(得分:1)

在跳跃的重音过程中,Y速度是完全的 被权力曲线所覆盖。在体面,重力需要 过度。跳跃速度由jumpTime字段控制 它将时间测量为当前跳跃的重点。

首先,你需要一些简单的全局变量,

public bool isJumping; //Is the player in a jump?
private bool wasJumping; //Did the player just exit a jump?
private float jumpTime; //Time the player has been in a jump (Useful for adding a power curve or to max out on jump height)

MaxJumpTime = .8f; //If you want to max out a jump, otherwise remove the relevant parts of the code
JumpLaunchVelocity = -3000.0f; //How much velocity to "jump" with, this may depend on your speed of your player, so it will need some ajustment

以下是完成大部分工作的功能:

 private float DoJump(float velocityY, GameTime gameTime)
        {
            // If the player wants to jump
            if (isJumping)
            {
                // Begin or continue a jump
                if ((!wasJumping && IsOnGround) || jumpTime > 0.0f)
                {
                    //Do things like animate, or play a sound here
                    jumpTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
                }

                // If we are in the ascent of the jump
                if (0.0f < jumpTime && jumpTime <= MaxJumpTime)
                {
                    // Fully override the vertical velocity with a power curve that gives players more control over the top of the jump (If you dont want this you can remove the Math.Pow)
                    velocityY = JumpLaunchVelocity * (1.0f - (float)Math.Pow(jumpTime / MaxJumpTime, JumpControlPower));
                }
                else
                    jumpTime = 0.0f;   // Reached the apex of the jump
            }
            else
            {
                // Continues not jumping or cancels a jump in progress
                jumpTime = 0.0f;
            }
            wasJumping = isJumping;

            return velocityY;
        }

在计算位置和内容时的更新中:

velocity.Y = DoJump(velocity.Y, gameTime);

如果遇到任何问题,请查看Platformer Starter Kit