我在我正在制作的平台游戏中遇到了我的世界碰撞问题。我知道为什么(或者我认为我知道)问题会出现。我的程序似乎进入了第一个“if”,当时它也没有。有些东西是错的(我想),但我找不到。
其次,我想知道我的解决方案是否是一个好主意,或者是否应该采取其他措施。如果是这样的错误?我是编程的初学者,所以请温柔地对待我; D
protected void CheckWorldCollison()
{
float collisionWidth = this.width + (Tile.GetTileSize() * 2);
float collisionHeight = this.height + (Tile.GetTileSize() * 2);
for (int x = -1; x < (collisionWidth / Tile.GetTileSize()) - 1; x++)
{
for (int y = -1; y < (collisionHeight / Tile.GetTileSize()) - 1; y++)
{
Vector2 tempTilePosition = new Vector2((int)(this.position.X / Tile.GetTileSize()) + x, (int)(this.position.Y / Tile.GetTileSize()) + y);
if (tempTilePosition.X >= 0 && tempTilePosition.X < (Game1.testMap.GetWidth())
&& tempTilePosition.Y >= 0 && tempTilePosition.Y < (Game1.testMap.GetHeight()))
{
this.tilesToCheck.Add(Game1.testMap.GetTile(tempTilePosition));
}
}
}
foreach (Tile t in this.tilesToCheck)
{
t.setIsCkecked(true);
if (t.GetTileType() != TileType.transparent)
{
Vector2 distance = this.position - t.GetPosition();
//Console.WriteLine(distance);
Console.WriteLine((distance + this.origin).X);
if (((distance + this.origin).X >= 0 && (distance + this.origin).X <= Tile.GetTileSize())
|| ((distance - this.origin).X >= 0 && (distance - this.origin).X <= Tile.GetTileSize()))
{
Console.WriteLine((distance + this.origin).Y);
if ((distance + this.origin).Y <= 0)
{
if (this.speed.Y > 0)
{
if ((distance + this.origin + this.speed).Y > 0)
{
this.position.Y = (t.GetPosition() - this.origin).Y;
this.speed.Y = 0;
}
}
}
if (((distance - this.origin).Y - Tile.GetTileSize()) <= 0)
{
if (this.speed.Y < 0)
{
if (((distance - this.origin + this.speed).Y - Tile.GetTileSize()) < 0)
{
this.position.Y = (t.GetPosition() + this.origin).Y + Tile.GetTileSize();
this.speed.Y = 0;
}
}
}
}
if (((distance + this.origin).Y >= 0 && (distance + this.origin).Y <= Tile.GetTileSize())
|| ((distance - this.origin).Y >= 0 && (distance - this.origin).Y <= Tile.GetTileSize()))
{
if ((distance + this.origin).X <= 0)
{
if (this.speed.X > 0)
{
if ((distance + this.origin + this.speed).X > 0)
{
this.position.X = (t.GetPosition() - this.origin).X;
this.speed.X = 0;
}
}
}
if (((distance - this.origin).X - Tile.GetTileSize()) >= 0)
{
if (this.speed.X < 0)
{
if (((distance - this.origin + this.speed).X - Tile.GetTileSize()) < 0)
{
this.position.X = (t.GetPosition() + this.origin).X + Tile.GetTileSize();
this.speed.X = 0;
}
}
}
}
}
}
this.tilesToCheck.Clear();
}
只是想说我猜这些问题一直都在这里突然出现,但如果我真的不需要帮助,我就不会写。