我正在努力做到这一点,以便我的球员在击球时完全停止,但如果他按下某个键,他可能会变得不卡住。示例:玩家正在下降并撞到了一个障碍物。他停下来,但如果按 W、A 或 D,他可以向上、向左或向右移动。这是我目前拥有的代码。
在这种情况下,sp 表示速度,blocksp 表示他被阻挡时的速度。(This.Sprite),在这种情况下,指的是阻止他的块。
bool Blocked = false;
float Bottom = -32;
float Left = -32;
float Right = This.Sprite.GetWidth() - 32;
float Top = This.Sprite.GetHeight() - 32;
int x = 0;
int y = 0;
float sp = 1.59f;
float blocksp = 0.00f;
Sprite Player = This.Game.FindSprite("GuySprite");
if (This.Sprite.CollisionWithSprite("GuySprite") != null)
{
if (Player.Position.Y > Top)
{
Blocked = true;
Player.Velocity = new Point2D(x, y) * blocksp;
Player.Animation = 0;
if (This.Game.IsPressed(InputKey.W) || This.Game.IsPressed(InputKey.A) || This.Game.IsPressed(InputKey.D))
{
Blocked = false;
Player.Velocity = new Point2D(x, y) * sp;
Player.Position.Y -= 0.85f;
}
}
if (Player.Position.Y < Bottom)
{
Blocked = true;
Player.Velocity = new Point2D(x, y) * blocksp;
Player.Animation = 0;
if (This.Game.IsPressed(InputKey.S) || This.Game.IsPressed(InputKey.A) || This.Game.IsPressed(InputKey.D))
{
Blocked = false;
Player.Velocity = new Point2D(x, y) * sp;
Player.Position.Y += 0.85f;
}
}
if (Player.Position.Y < Right)
{
Blocked = true;
Player.Velocity = new Point2D(x, y) * blocksp;
Player.Animation = 0;
if (This.Game.IsPressed(InputKey.S) || This.Game.IsPressed(InputKey.A) || This.Game.IsPressed(InputKey.W))
{
Blocked = false;
Player.Velocity = new Point2D(x, y) * sp;
Player.Position.Y -= 0.85f;
}
}
if (Player.Position.Y > Left)
{
Blocked = true;
Player.Velocity = new Point2D(x, y) * blocksp;
Player.Animation = 0;
if (This.Game.IsPressed(InputKey.S) || This.Game.IsPressed(InputKey.W) || This.Game.IsPressed(InputKey.D))
{
Blocked = false;
Player.Velocity = new Point2D(x, y) * sp;
Player.Position.X += 0.85f;
}
}
}
目前,如果我的玩家接触到方块的任何一侧并继续移动到方块停止,那么玩家将朝一个不稳定的方向移动并且不会在方块处停止。有任何想法吗?