我的跳转按钮不起作用? [英] My jump button won't work?
问题描述
大家好,
我在XNA C#中创建游戏,我目前正在创建跳转功能。但由于某些原因它不起作用?
按下W时触发我的Jump()。我的Gravity()每秒触发约55次。 br $> b $ b
Hello everyone,
I am creating a game in XNA C#, and I am currently creating the jump function. But for some reason it won't work?
My Jump() is triggered when pressed W. And my Gravity() is triggered about 55 times a second.
/* Is the player jumping? */
public bool Jumping = true;
public void Jump()
{
if (Jumping == false)
{
Jumping = true;
for (int i = 0; i < 8; i++)
{
if (!Colliding(0, i))
{
this.PositionPixelY -= i;
CalculatePositions();
}
}
}
}
public void Gravity()
{
if (!Colliding(0, MovingSpeed) && !Jumping)
{
this.PositionPixelY += MovingSpeed;
CalculatePositions();
}
if(Colliding(0, MovingSpeed))
{
Jumping = false;
}
}
编辑:此外,当我的角色陷入困境时,我跳的是什么没有重力拉下来。对不起,我总是忘记说出了什么问题。
Also, what happens is, when I jump once my character gets stuck with no gravity pulling it down. Sorry I always forget to say what goes wrong.
推荐答案
好吧我遇到的问题是,一旦我们跳了就不会失败。
这是正常的,因为我只是告诉它一旦我们不再跳跃就会失败。
然而,一旦我们到达地面,我们就不会跳跃。一个简单的解决方案是添加第二个布尔值,如下所示:
Okay the problem I was having is that it won't go down once we jumped.
This is normal because I am only telling it to go down once we're no longer jumping.
However, we won't be jumping once we reached the ground. A simple solution would be adding a second boolean like so:
public void Jump()
{
if (Jumping == false)
{
GravityBool = false;
Jumping = true;
for (int i = 0; i < 8; i++)
{
if (!Colliding(0, -i))
{
this.PositionPixelY -= i;
CalculatePositions();
}
}
GravityBool = true;
}
}
public void Gravity()
{
if (!Colliding(0, MovingSpeed) && GravityBool)
{
this.PositionPixelY += MovingSpeed;
CalculatePositions();
}
else
{
Jumping = false;
}
}
再次感谢Codeproject让我的想法通过上传这个问题来节省一些时间,我稍后将其整理出来。
Thanks Codeproject once again for letting my mind having a bit of peace by uploading this problem, and I sorted it out a bit later.
这篇关于我的跳转按钮不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!