缩小玩家坐标,使其适合2d阵列进行碰撞



我马上开始,我已经使用纹理列表和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;
    }

您似乎试图通过玩家位置访问2D数组。你的瓷砖是60x60px,所以如果你的玩家在coord:x=30;例如y=40,它应该在第一个瓦片[0,0]中,因为它们都<60.

在这里,您尝试询问您的数组索引[30,40],它超出了界限,但正确的瓦片编号是您的位置/tileSize

所以,

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;
}

变为:

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 / tiles.tileWidth, y / tiles.tileHeight] == 1) return true;
    else return false;
}

最新更新