【发布时间】:2016-05-13 14:20:31
【问题描述】:
我创建了我的瓦片地图和我的移动玩家。
我现在正在尝试创建碰撞,我觉得我在正确的轨道上。
这是我创建地图的方式。
List<Texture2D> tileTextures = new List<Texture2D>();
int tileWidth = 60;
int tileHeight = 60;
public int[,] Map = new int[,]
{
{2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,1,2,2,2,2,2},
{2,2,2,2,2,2,2,2,2,2},
{2,2,2,2,2,2,2,2,2,2},
};
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);
}
}
}
我正在使用此条件检查 2d 数组坐标,并检查是否存在特定的图块,如果它是真的,我可以在其中设置我以前的位置。
我目前正在 1 个 tile atm 上进行测试。
public void Update(GameTime gameTime)
{
prevPosition = position;
input(gameTime);
if(tiles.Map[(int)playerPosition.X/60,(int)playerPosition.Y/60] == 1)
{
position = prevPosition;
}
}
但是,我的玩家位置一直超出 2D 数组的索引范围,我认为我需要将其缩小以阻止这种情况,我尝试将播放坐标除以图块的宽度,但这并没有没用。
如果有人可以帮助我进行正确的缩放,我将不胜感激。
【问题讨论】:
-
您是否检查过尝试此操作时获得的值?此外,您的地图不是 60x60,而是 4x10,当您尝试将播放器放置在该位置时,这可能会导致问题。
-
不,我还没有检查过我对这一切都很陌生,不确定如何检查。我也尝试过除以 10 和 4,我也遇到了同样的问题。有什么想法吗?
-
做一些谷歌搜索并学习如何使用调试器用于 Visual Studio 和即时窗口。许多正在开发的软件都在试图弄清楚为什么某些事情会发生意想不到的事情
标签: c# xna collision scaling bounds