Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.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
Java 尝试遍历2d数组,但未获得正确的值?_Java_If Statement_Multidimensional Array_Int_Paint - Fatal编程技术网

Java 尝试遍历2d数组,但未获得正确的值?

Java 尝试遍历2d数组,但未获得正确的值?,java,if-statement,multidimensional-array,int,paint,Java,If Statement,Multidimensional Array,Int,Paint,所以现在我正在做一个迷宫游戏,当然要想成为一个迷宫游戏,它必须有墙。墙需要阻止玩家越过墙。我在检查碰撞时遇到问题。它在某些地方起作用,而在其他地方则不起作用。我当前的方法是通过我的2d数组,通过将玩家当前的x和y除以50,然后使用这两个数字来尝试查看玩家是否会撞到墙,从而找到玩家试图继续的数字。发生的事情是,它阻止玩家移动某些墙,而有些墙则没有。此外,它还会在没有墙的地方停止玩家(值为2)。我觉得数学有问题,但我想不出是什么。下面是我如何制作迷宫的代码,以及制作迷宫的阵列: private in

所以现在我正在做一个迷宫游戏,当然要想成为一个迷宫游戏,它必须有墙。墙需要阻止玩家越过墙。我在检查碰撞时遇到问题。它在某些地方起作用,而在其他地方则不起作用。我当前的方法是通过我的2d数组,通过将玩家当前的x和y除以50,然后使用这两个数字来尝试查看玩家是否会撞到墙,从而找到玩家试图继续的数字。发生的事情是,它阻止玩家移动某些墙,而有些墙则没有。此外,它还会在没有墙的地方停止玩家(值为2)。我觉得数学有问题,但我想不出是什么。下面是我如何制作迷宫的代码,以及制作迷宫的阵列:

private int[][] mazeWalls = { //top of the maze
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
        {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
        {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
        {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
        {1, 1, 1, 1, 2, 2, 2, 2, 2, 1},
/*left side*/{1, 2, 2, 2, 2, 2, 2, 2, 2, 1}, //right side
        {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
        {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
        {1, 2, 2, 2, 2, 2, 2, 1, 2, 1},
        {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
                                //bottom of the maze
};

public void paintMaze(Graphics g){
    for(int row = 0; row < mazeWalls.length; row++){ //example of loops
        for(int col = 0; col < mazeWalls[row].length; col++){ //getting the positions set up
            if(mazeWalls[row][col] == 1){
                g.setColor(Color.RED); //color of the walls
                g.fillRect(col * 50, row * 50, 50, 50); //col times 50 is the x coordinate, and row times 50 is the y coordinate
            }
        }
    }
}

我觉得它应该是有效的,但出于某种原因(我认为它是在划分玩家坐标后的数字,或者其他什么),它不是。

2D数组中的第一个索引按惯例是行索引,第二个是列索引,所以你的坐标是错误的:

public void collisionChecker(int playerX, int playerY){ //takes the user's location and then checks if they are running into the wall
    if(mazeWalls[playerY / 50][playerX / 50] == 1 ){
        setCollision(true);
    }
    else if(mazeWalls[playerY / 50][playerX / 50] != 1){
        setCollision(false); //just in case
    }
}
此代码可以简化为

public void collisionChecker(int playerX, int playerY){ //takes the user's location and then checks if they are running into the wall
    setCollision(mazeWalls[playerY / 50][playerX / 50] == 1);
}

我相信
playerX
playerY
是用户单击的坐标,然后您试图获取矩形角左上角的坐标,以确定碰撞与否。对吗?playerX和playerY的实际值是多少,它们总是可以被50整除吗?如果不是的话,那可能就是问题所在。我原本想这样做,但我决定这样做不会有任何效果(显然我错了,不知道为什么我没有尝试)。我仍然可以越过一些墙,但在这一点上,这似乎是不可避免的,因为我正在这样做。如果你知道为什么会发生这种情况,请告诉我,但你的解决方案确实解决了问题。如果你在玩家穿越某些墙时仍然遇到问题,请尝试考虑玩家的宽度和高度,而不是只测试一个点(playerX,playerY)。只要玩家的尺寸小于一平方米,你就可以测试(playerX+playerWidth,playerY),(playerX,playerY+playerwhight)和(playerX+playerWidth,playerwhight+playerwhight),假设playerX,playerY是你们的播放器Spritie的左上角,实际上我刚刚意识到我在某些方向上打开了碰撞检查,而在其他方向上,我把它注释掉了。这就是我为什么要翻墙的原因。现在可以用了,谢谢!
public void collisionChecker(int playerX, int playerY){ //takes the user's location and then checks if they are running into the wall
    setCollision(mazeWalls[playerY / 50][playerX / 50] == 1);
}