跳跃与。重力
我正在开发我的第一个 XNA 2D 游戏,但遇到了一些问题。 如果我跳,我的精灵会跳但不会掉下来。 我还有另一个问题,用户可以按住空格键跳到他想要的高度,但我不知道如何阻止他这样做。这是我的代码: 跳跃:
if (FaKeyboard.IsKeyDown(Keys.Space))
{
Jumping = true;
xPosition -= new Vector2(0, 5);
}
if (xPosition.Y >= 10)
{
Jumping = false;
Grounded = false;
}
非常简单的基本重力:
if (!Grounded && !Jumping)
{
xPosition += new Vector2(1, 3) * speed;
}
这里是接地设置为 True 或 False 的碰撞
Rectangle MegamanRectangle = new Rectangle((int)xPosition.X, (int)xPosition.Y, FrameSizeDraw.X, FrameSizeDraw.Y);
Rectangle Block1Rectangle = new Rectangle((int)0, (int)73, Block1.Width, Block1.Height);
Rectangle Block2Rectangle = new Rectangle((int)500, (int)73, Block2.Width, Block2.Height);
if ((MegamanRectangle.Intersects(Block1Rectangle) || (MegamanRectangle.Intersects(Block2Rectangle))))
{
Grounded = true;
}
else
{
Grounded = false;
}
接地布尔和重力已经过测试并且正在工作。 有什么想法吗? 预先致谢,如果您需要本准则的另一部分,请随时询问。
I'm working on my first XNA 2D game and I have a little problem.
If I jump, my sprite jumps but does not fall down.
And I also have another problem, the user can hold spacebar to jump as high as he wants and I don't know how to keep him from doing that. Here's my code:
The Jump :
if (FaKeyboard.IsKeyDown(Keys.Space))
{
Jumping = true;
xPosition -= new Vector2(0, 5);
}
if (xPosition.Y >= 10)
{
Jumping = false;
Grounded = false;
}
The really simple basic Gravity:
if (!Grounded && !Jumping)
{
xPosition += new Vector2(1, 3) * speed;
}
Here's where's the grounded is set to True or False with a Collision
Rectangle MegamanRectangle = new Rectangle((int)xPosition.X, (int)xPosition.Y, FrameSizeDraw.X, FrameSizeDraw.Y);
Rectangle Block1Rectangle = new Rectangle((int)0, (int)73, Block1.Width, Block1.Height);
Rectangle Block2Rectangle = new Rectangle((int)500, (int)73, Block2.Width, Block2.Height);
if ((MegamanRectangle.Intersects(Block1Rectangle) || (MegamanRectangle.Intersects(Block2Rectangle))))
{
Grounded = true;
}
else
{
Grounded = false;
}
The grounded bool and The gravity have been tested and are working.
Any ideas why?
Thanks in advance and don't hesitate to ask if you need another Part of the Code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
处理跳跃的一种相对简单的方法是将重力实现为矢量,并将跳跃/移动实现为脉冲。
例如:
速度应该是与世界具有相同维度的向量,而Move是在给定向量中将对象移动到尽可能远的方法。
跳跃代码:
这将导致对象向上移动一点,然后开始下降。您可以将类似的脉冲用于其他效果(移动、爆炸、碰撞)。当两个物体碰撞时,您需要碰撞修改速度以进行弹跳。
这是一个极其简单的物理系统,将使未来的变化变得更加简单,并且可以允许一些有趣的关卡设计(改变重力等)。它通过将复杂的 IsJumping 代码抽象为单个机制来简化跳跃处理。
为了防止在不站在物体上的情况下跳跃,您需要进行碰撞测试,或跟踪物体向下碰撞/停止碰撞的时间。还可以检查物体的垂直速度是否为零并仅允许跳跃(防止在坠落或向上移动时跳跃)。
A relatively simple way to handle jumping is to implement gravity as a vector and jumping/moving as an impulse.
For example:
The velocity should be a vector with the same dimensions as the world, and Move a method that moves the object as far as possible in the vector given.
Jump code:
This will cause the object to move up for a bit, then begin to fall. You can use similar impulses for other effects (movement, explosions, collision). You will need to have collision modify the velocity when two objects collide, for bouncing.
This is an extremely simple physics system that will make future changes much simpler, and could allow some interesting level design (change gravity and such). It simplifies handling jumping by abstracting it out from complex IsJumping code to a single mechanic.
To prevent jumping without standing on an object, you'd need to do a collision test, or track when objects collide/stop colliding for the down direction. It might also be possible to check if the object's vertical velocity is zero and allow jumping only then (would prevent jumping while falling or moving up).
我相信这可能是答案:
将您的更改
为
速度给出的其他答案确实是更好的方法,但这可能足够接近,可以让您重新开始。
I believe this might be the answer:
change your
to
The other answers given with velocity are really better approaches, but this might be close enough to get you started again.
这是一个非常简单的解决方案,可以使跳跃向上和向下移动:
并通过以下方式激活它的作用
是通过将其设置为 1 来启动跳跃
另外,如果您想限制仅在角色位于地面时跳跃,您可以添加与此类似的条件
Here is a very rustic solution to making a jump go up and down:
And is activated by
What this does is set in motion the jump by setting it to 1
Also if you want to restrict jumping to only when the character is on the ground you can add a condition similar to this
跳跃不应该是布尔值,除非您需要它用于动画或只能在跳跃时使用的能力之类的东西。
相反,您应该使用矢量运动,当玩家接触地面并按下跳跃按钮时,为您的运动添加向上跳跃矢量。
然后,重力需要成为作用于玩家的向下矢量,并且它会自行处理。
Jumping shouldn't be a boolean unless you need it for something like an animation or an ability that can only be used while jumping.
Instead, you should be using vector movement and when the player is touching the ground and press their jump button, add an upward jump vector to your motion.
Then, gravity needs to be a downward vector acting against the player and it will handle itself.