【问题标题】:Downscaling player coords so they fit in 2d array for collision缩小玩家坐标,使其适合二维数组以进行碰撞
【发布时间】: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


    【解决方案1】:

    您似乎试图按玩家位置访问您的二维数组。 你的图块是 60x60px,所以如果你的玩家处于坐标位置:x=30; y=40 例如, 它应该在第一个图块 [0,0] 中,因为它们都是

    在这里,您尝试询问您的数组索引 [30,40],这是超出范围的,但正确的图块编号是您的 position / 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;
    }
    

    【讨论】:

    • 我在他最后一个问题中给了他那个函数,我没有看到那个错误。 +1 看到@Vernier
    • 我还想提醒@Spoingen 批准你在这个问题中的回答和我在他最后一个问题中的回答
    • 谢谢你的好先生,我只是很抱歉迟到了直到现在才能继续。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    相关资源
    最近更新 更多