玩家与 2D 阵列瓦片地图的碰撞
Player collision with 2D array tilemap
我目前已经用二维数组绘制了我的瓷砖地图,我的角色也在移动和绘制。
我现在正在尝试解决碰撞方面的问题,我希望将玩家位置与瓦片地图坐标相关联,如果它们相等,则执行必要的碰撞操作。
但是我尝试将玩家位置向量与二维数组进行比较,但我不断收到诸如 "Cannot implicitly convert int to bool"
之类的错误
我想知道是否有人可以帮助我解决我需要检查的这种情况。
产生编译错误的代码是if(player.Position == Tiles.Map[(int)player.Position.X, (int)playerPosition.Y])
这是我绘制地图的方式:
public int[,] Map = new int[,]
{
{0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
public void Draw(SpriteBatch spriteBatch)
{
int tileMapWidth = Map.GetLength(1);
int tileMapHeight = Map.GetLength(0);
for (int x = 0; x < tileMapWidth; x++)
{
for (int y = 0; y < tileMapHeight; y++)
{
int textureIndex = Map[y, x];
Texture2D texture = tileTextures[textureIndex];
spriteBatch.Draw(
texture,
source = new Rectangle(x * myTile.Width,
y * myTile.Height,
tileWidth,
tileHeight),
Color.White);
}
}
}
您正在尝试将玩家位置与整数数组进行比较,但您想要的是检查数组是否具有“1”作为玩家位置的值。
改变
if(player.Position == Tiles.Map[(int)player.Position.X, (int)playerPosition.Y])
至
if(Tiles.Map[(int)player.Position.X, (int)playerPosition.Y] == 1)
否则,如果你知道地图的具体位置(我们称它们为 X 和 Y),则根本不涉及数组,它应该是这样的:
if((int)player.Position.X == X && (int)playerPosition.Y == Y)
我目前已经用二维数组绘制了我的瓷砖地图,我的角色也在移动和绘制。
我现在正在尝试解决碰撞方面的问题,我希望将玩家位置与瓦片地图坐标相关联,如果它们相等,则执行必要的碰撞操作。
但是我尝试将玩家位置向量与二维数组进行比较,但我不断收到诸如 "Cannot implicitly convert int to bool"
我想知道是否有人可以帮助我解决我需要检查的这种情况。
产生编译错误的代码是if(player.Position == Tiles.Map[(int)player.Position.X, (int)playerPosition.Y])
这是我绘制地图的方式:
public int[,] Map = new int[,]
{
{0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0},
};
public void Draw(SpriteBatch spriteBatch)
{
int tileMapWidth = Map.GetLength(1);
int tileMapHeight = Map.GetLength(0);
for (int x = 0; x < tileMapWidth; x++)
{
for (int y = 0; y < tileMapHeight; y++)
{
int textureIndex = Map[y, x];
Texture2D texture = tileTextures[textureIndex];
spriteBatch.Draw(
texture,
source = new Rectangle(x * myTile.Width,
y * myTile.Height,
tileWidth,
tileHeight),
Color.White);
}
}
}
您正在尝试将玩家位置与整数数组进行比较,但您想要的是检查数组是否具有“1”作为玩家位置的值。
改变
if(player.Position == Tiles.Map[(int)player.Position.X, (int)playerPosition.Y])
至
if(Tiles.Map[(int)player.Position.X, (int)playerPosition.Y] == 1)
否则,如果你知道地图的具体位置(我们称它们为 X 和 Y),则根本不涉及数组,它应该是这样的:
if((int)player.Position.X == X && (int)playerPosition.Y == Y)