【发布时间】:2016-05-13 23:09:53
【问题描述】:
我会直接进入它我已经使用纹理列表和 2D 数组创建了一个地图,我已经绘制了玩家和运动,现在我正在尝试使用我的玩家坐标来访问数组中的一个位置检查它是否有一个特定的瓷砖,如果它确实让我的玩家回到他以前的位置,产生一种碰撞。
但是我遇到的问题是,当我将播放器坐标转换为数组时,它要么超出范围,要么对于实际数组来说太高,我需要以某种方式缩小我的坐标,以便它们与数组准确。
这是我绘制瓷砖的方式
int tileWidth = 60;
int tileHeight = 60;
List<Texture2D> tileTextures = new List<Texture2D>();
public int[,] Map = new int[,]
{
{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,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,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},
{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,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);
}
}
}
这是我的玩家动作
public void input(GameTime gameTime)
{
KeyboardState ks = Keyboard.GetState();
Vector2 motion = Vector2.Zero;
if (ks.IsKeyDown(Keys.W))
motion.Y--;
if (ks.IsKeyDown(Keys.S))
motion.Y++;
if (ks.IsKeyDown(Keys.D))
{
currentAnim = rightWalk;
Animate(gameTime);
motion.X++;
}
if (ks.IsKeyDown(Keys.A))
{
currentAnim = leftWalk;
Animate(gameTime);
motion.X--;
}
if (motion != Vector2.Zero)
motion.Normalize();
position += motion * speed;
}
最后是我的播放器更新
public void Update(GameTime gameTime)
{
prevPosition = position;
input(gameTime);
if(CollidesWithWall((int)position.X, (int)position.Y))
{
position = prevPosition;
}
}
还有我的 CollidingWithWall 函数
public bool CollidesWithWall(int x, int y)
{
if (x < 0 || x > (15) -1) return true;
if (y < 0 || y > (7) -1) return true;
if (tiles.Map[x, y] == 1) return true;
else return false;
}
【问题讨论】:
标签: c# multidimensional-array xna collision scaling