我正在使用Windows Form Application并制作一个简单的2d平台游戏,其中包含作为播放器的图片框和作为块的图片框的数组列表。 当我与Blocks [i]碰撞时。顶部的“玩家”一直落入顶部区域。我认为这是因为
{player.Top += 3;}
总是在发生。
当我与顶部碰撞碰撞时,如何告诉玩家停止摔倒?
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < blocks.Count; i++)
{
Rectangle playerRect = player.ClientRectangle;
playerRect.X = player.Location.X;
playerRect.Y = player.Location.Y;
Rectangle blockRect = blocks[i].ClientRectangle;
blockRect.X = blocks[i].Location.X;
blockRect.Y = blocks[i].Location.Y;
Rectangle Intersection = Rectangle.Intersect(playerRect, blockRect);
if (jump == true)
{
//hur snabbt spelaren faller
player.Top -= force;
force -= 1;
}
if (player.Top + player.Height >= screen.Height)
{
player.Top = screen.Height - player.Height;
if (jump == true)
{
player.Image = Image.FromFile("gubbe_still.png");
}
jump = false;
}
else
{
player.Top += 3;
}
//sid kollision
if (player.Right > blocks[i].Left &&
player.Left < blocks[i].Right - player.Width / 2 &&
player.Bottom > blocks[i].Top)
{
right = false;
}
//sid kollision
if (player.Left < blocks[i].Right &&
player.Right > blocks[i].Left + player.Width / 2 &&
player.Bottom > blocks[i].Top)
{
left = false;
}
// top kollision
if (player.Right - 5 > blocks[i].Left &&
player.Right + 5 < blocks[i].Left + blocks[i].Width + player.Width &&
player.Top + player.Height >= blocks[i].Top &&
player.Top < blocks[i].Top)
{
jump = false;
force = 0;
player.Top = blocks[i].Location.Y - player.Height;
}
}
答案 0 :(得分:0)
您有一些碰撞检测问题
// top kollision
if (player.Right - 5 > blocks[i].Left &&
player.Right + 5 < blocks[i].Left + blocks[i].Width + player.Width &&
player.Top + player.Height >= blocks[i].Top &&
player.Top < blocks[i].Top)
如果玩家完全站在禁区左侧,第一次检查(player.Right - 5&gt; blocks [i] .Left)将会失败。你想要这样的东西:
// top kollision
if ((player.Left > blocks[i].Left &&
player.Left < blocks[i].Right) ||
(player.Right < blocks[i].Left &&
player.Right > blocks[i].Left) &&
player.Top + player.Height >= blocks[i].Top &&
player.Top < blocks[i].Top)