XNA - 保持更长时间跳得更高 [英] XNA - Hold longer to jump higher
问题描述
我正在为我的游戏寻找一种简单的方法,这样当你按住空格键时,你会跳得更高.当您点击"时,您不会跳到最大高度.需要有一个最大值,但是是的,我不知道如何进行编程.
I'm looking for a simple method for my game, so that when you hold down the space bar, you jump higher. When you 'tap' you don't jump to maximum height. There would need to be a max, but yeah, I don't know how to go about programming this.
任何帮助,非常感谢.将给予反馈.谢谢.
Anyhelp, very appreciated. Feedback will be given. Thanks.
推荐答案
在你处理跳跃的更新函数中,你可以让它跟踪角色离开地面的时间,并让它的向上速度停止增加时间.
In your update function that handles jumping, you could have it keep track of how long the character has been off the ground and have it's upward velocity stop increasing after an amount of time.
例如,像下面这样的东西应该可以工作
For instance, something like the following should work
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;
}
}
这篇关于XNA - 保持更长时间跳得更高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!