【问题标题】:First isometric game第一个等距游戏
【发布时间】:2013-11-09 10:18:20
【问题描述】:

我的第一个等距游戏有问题。我不知道该怎么做才能让我的球员接近墙的边缘。此时玩家可能会在绿色区域内移动。

我的地图:

int[,] map = new int[,] 
        {

            {1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 0, 0, 1},
            {1, 1, 1, 1, 1, 1, 1, 1}

        }; 

变量:

int TileWidth = 50;
int TileHeight = 50;
int posX = 2; // map X position
int posY = 2; // map Y position
float playerX = 2 * 50; // player X position
float playerY = 2 * 50; // player Y position

检测墙:

    public bool detectSolidTile(int x, int y)
    {

        if (map[y, x] == 1) return true;  else return false;

    }

移动:

posX = (int)(Math.Floor((playerX) / 50));
posY = (int)(Math.Floor(playerY / 50));

(...)

        if (slide == 1 && !detectSolidTile(posX + 1, posY))
        {
            playerX++;
        }
        if (slide == 2 && !detectSolidTile(posX - 1, posY))
        {
            playerX--;
        }

图片 -> http://s16.postimg.org/cxkfomemd/tiles.jpg

我需要改进什么才能在墙之间移动?

最好的问候,克日谢克

【问题讨论】:

    标签: c# tiles isometric


    【解决方案1】:

    尝试将所有可以导航的地图设为 0,然后将无法导航的地图设为 1。 当尝试进行新的移动时,检查未来的位置是 1 还是 0。如果是 0,你让他移动,否则你阻止他。

        if (slide == 1 && !detectSolidTile(posX + 1, posY))
        {
            playerX++;
        }
    

    这会将带有 1 的地图检测为在可移动区域之外,从而阻止它去你想要的地方。

    你知道现在问题出在哪里了吗?

    另一种解决方案是了解矩阵的大小,一旦 x 或 y 达到最大值,就停止递增它们。但是如果你以后想添加障碍,继续你现在做的,但确保0是地图,1是地图外。

    所以这里是 BartoszKP 的编辑: 你可能是对的,这个“!”没有修复他的代码,我只是在解释他应该怎么做。

    我将用于他的问题的代码略有不同。

    首先删除 playerX 和 playerY,因为它与 posX 和 posY 完全相同,你只需进行额外的计算。 现在有人说你的运动算法看起来像这样:

    变量:

    int TileWidth = 50; 
    int TileHeight = 50;
    int posX = 2; // map X position - same thing as playerX 
    int posY = 2; // map Y position - same thing as playerY 
    //posX and posY will now hold the position of your player on the map since your conversion from playerX to playerY will only work if you increment a value by 50 or more.
    

    检测墙壁: //如果坐标 x 和 y 处的瓷砖是墙,我返回 true //否则返回假 公共布尔检测SolidTile(int x,int y) {

        if (map[y, x] == 1) return true;
        else return false;
    }
    

    运动:

    posX = (int)(Math.Floor((playerX) / 50)); //drop this you don't need it
    posY = (int)(Math.Floor(playerY / 50)); //drop this too, you are using posX and posY to store locations
    
    (...)
    
            if (slide == 1 && !detectSolidTile(posX + 1, posY))
            {
                posX++;
            }
            if (slide == 2 && !detectSolidTile(posX - 1, posY))
            {
                posX--;
            }
    
            if (slide == 3 && !detectSolidTile(posX, posY+1))
            {
                posY++;
            }
            if (slide == 4 && !detectSolidTile(posX, posY-1))
            {
                posY--;
            }
    

    如果您使用 posX 和 posY 作为玩家在地图上的位置,这应该可以很好地工作。 为玩家制作一种类型的坐标,为地图制作一种类型的坐标只会让此时更加混乱。但如果您确实需要为它们使用不同的坐标,则在尝试计算运动时应始终参考 playerX 和 playerY,如下所示:

    if (slide == 1 && !detectSolidTile((int)(Math.Floor((playerX+1) / 50)) + 1, posY))
    {
        playerX++;
    } 
    

    这是因为您将 playerX 的值更改为 1 而不是 posX 的值,这将限制您的移动。如果这令人困惑,请告诉我,我会尝试更好地解释这一点。

    【讨论】:

    • 将 1 更改为 0 有帮助,但只有右墙。玩家只在绿色区域移动 :( IMG i.stack.imgur.com/k6xG8.jpg 编辑:我做了 :DI 改变 if (slide == 2 && detectSolidTile(posX - 1, posY)) 到 if (slide == 2 && detectSolidTile(posX, posY))
    • 对 y 使用相同的过程可以让它上下移动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 2021-09-21
    相关资源
    最近更新 更多