-3

我在开发我的平台游戏时遇到了一个问题(再次:D),我真的找不到解决方案..所以这里是代码:

Blocks.AddRange(this.Controls.OfType<PictureBox>());
if (Blocks.Any(x => x.Location.Y - Player.Height <= Player.Location.Y))
{
    Player.Top = Height - ClosestBlock.Location.Y - Player.Height;
}

我需要检测哪个块最接近移动玩家。如果您有任何想法,请告诉我,谢谢!

4

1 回答 1

0

Check out the Phytagorean theorem.

If you have two points (x1, y1) and (x2, y2), then the distance between the two are distance = Math.Sqrt(Math.Pow((x2-x1), 2) + Math.Pow(y2-y1),2))

Iterate over all blocks, calculate their center-position (x2, y2) and use the Phytagorean theormen to find the distance between player center-position (x1, y1) and each block (x2n, y2n). Select the block with the shortest distance.

于 2015-12-26T16:25:04.937 回答