Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 第一等距对策_C#_Tiles_Isometric - Fatal编程技术网

C# 第一等距对策

C# 第一等距对策,c#,tiles,isometric,C#,Tiles,Isometric,我的第一个等距游戏有问题。我不知道如何才能让我的球员接近墙的边缘。在这一刻,玩家可能会在绿色区域移动 我的地图: 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},

我的第一个等距游戏有问题。我不知道如何才能让我的球员接近墙的边缘。在这一刻,玩家可能会在绿色区域移动

我的地图:

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
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.
检测墙:

    public bool detectSolidTile(int x, int y)
    {

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

    }
Movemet:

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

我需要改进什么才能从一堵墙移动到另一堵墙


致以最良好的祝愿,Krzysiek

尝试制作所有可以导航0的地图,然后制作不能导航1的地图。 尝试进行新移动时,请检查未来位置是1还是0。如果是0,你就让他移动,否则你就阻止他

    if (slide == 1 && !detectSolidTile(posX + 1, posY))
    {
        playerX++;
    }
这将检测到带有1的地图位于可移动区域之外,从而阻止它移动到您想要的地方

你知道问题在哪里吗

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

下面是BartoszKP的编辑: 你可能是对的,这个“!”并不能修复他的代码,我只是在解释他应该怎么做

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

首先,放下playerX和playerY,因为它与posX和posY完全相同,你只需要做额外的计算。 现在我们说你的运动算法会是这样的:

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

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
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 //否则返回false 公共布尔检测SolidFile(整数x,整数y) {

运动:

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编辑:我做了:D我更改如果(幻灯片==2&&detectSolidFile)(posX-1,posY))到if(slide==2&&detectSolidFile(posX,posY))使用与y相同的过程,可以使其上下移动。