简体   繁体   English

具有Rectangle()网格的2d数组

[英]2d Array with Rectangle() grid

I am trying to make a grid, using a 2d array list, I want to make it with Rectangles so I can use .intersects with it, I also need it to be 95 x 95 and 95 apart, this is what I have so far but its not working because of an error. 我正在尝试使用2d数组列表制作网格,我想使用Rectangles制作网格,以便可以与其使用.intersects,我还需要将其相隔95 x 95和95,这是我到目前为止所拥有的但由于错误而无法正常工作。

    public static Rectangle[][] walls;
    public static void walls() {
    int wallsY = 0, wallsX =0;
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 7; j++) {
            //shapeList.add(new Rectangle(wallsX, wallsY, 95, 95));
            walls[i][j] = new Rectangle(wallsX,wallsY,95,95);
            wallsY += 95;
            wallsX += 95;

        }
    }

then i use: 然后我用:

    for (int i = 0; i < walls.length; i++) {
        for(int j =0; j < walls.length; j++){
            if (intersectsBox(playerRectangle(), walls[i][j])) {
                isInsideWalls = true;
            }   
        }
     }

to check if they are intersecting. 检查它们是否相交。 But I keep getting an error which is right here: 但是我一直收到一个错误,就在这里:

`Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at bombermangame.Game.walls(Game.java:165) at bombermangame.Game.(Game.java:62) at bombermangame.Menu.actionPerformed(Menu.java:98) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source `线程“ AWT-EventQueue-0”中的异常在bombermangame.Game.walls(Game.java:165)在bombermangame.Game。(Game.java:62)在bombermangame.Menu.actionPerformed(Menu。 java:98)在javax.swing.AbstractButton.fireActionPerformed(未知源

You never initialized walls. 您从未初始化墙。 Prior to your for loop for (int i = 0; i < 7; i++) , add: 在for循环for (int i = 0; i < 7; i++) ,添加:

walls = new Rectangle[7][7];

I put 7 and 7 in there because that is what it appears the dimensions will be. 我将7和7放在那里,因为这就是尺寸的样子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM