【发布时间】:2017-12-04 19:45:43
【问题描述】:
我已经尝试寻找答案,但我没有找到任何东西。我正在用 c# 制作这个 2d 游戏,我希望玩家被每次调用函数时(在 while 循环内)通过重力增加的力拉下。由于某种原因,当 for 循环中断时,代码不会停止。
private void PhysicsLoop()
{
NowX = Convert.ToInt32(Math.Floor(Convert.ToDecimal(Player.x / 32)));
NowY = Convert.ToInt32(Math.Floor(Convert.ToDecimal(Player.y / 32)));
if (Block[NowX,NowY + 2].Collides == false)
{
Falling = true;
}
if (Falling == true)
{
force += Gravity;
for (int i = 0; i <= force; i++)
{
if (Block[NowX, NowY + 2].Collides == true)
{
Falling = false;
break;
}
else
{
Player.y += 1;
}
}
}
else
{
}
}
【问题讨论】:
-
force += Gravity;这应该在你的 for 循环中吗?我建议首先设置断点并使用调试器.. -
你说的“代码不会停止”是什么意思?您提到此方法是在
while循环内调用的。听起来这就是问题所在,因为您上面显示的代码看起来不错(除了不必要的空else块)。
标签: c# game-physics