简体   繁体   English

与rectangular.intersects碰撞[Java]

[英]collision with rectangle.intersects [Java]

So, I'm trying to make a collision for the walls in my game using Rectangles, and I decided to try and use an ArrayList to store the rectangles of each wall, and I make the entire field a wall, and all I want to do is remove three of the walls, so I'm doing shapeList.remove(0) to try and remove the first wall at 0,0 but its not working, I don't know if i'm doing it wrong, or if theres a better way, but I could use some help solving this issue, here is the code. 因此,我尝试使用Rectangles在我的游戏中对墙壁进行碰撞,我决定尝试使用ArrayList来存储每个墙壁的矩形,然后将整个字段变成一面墙壁,而我想要做的是删除三堵墙,所以我正在执行shapeList.remove(0)尝试删除位于0,0处的第一堵墙,但它不起作用,我不知道我做错了还是有更好的方法,但是我可以使用一些帮助解决此问题的方法,这是代码。

    public void walls(Graphics g) {
    for (int i = 0; i < 63; i++) {
        for (int wallsX = 0; wallsX < 750; wallsX += 95) {
            for (int wallsY = 0; wallsY < 750; wallsY += 95) {
                shapeList.add(new Rectangle(wallsX, wallsY, 95, 95));
                g.setColor(Color.blue);
                g.drawRect(wallsX, wallsY, 95, 95);
            }
        }
    }
    shapeList.remove(0); //I want to remove wall at 0,0... but not working
    g.setColor(Color.black);
    g.fillRect(0, 0, 95, 95);
    g.fillRect(95, 0, 95, 95);
    g.fillRect(0, 95, 95, 95);

    for (int i = 0; i < shapeList.size(); i++) {
        if (intersectsBox(getRectangle(), shapeList.get(i))) {
            isInsideWalls = true;

        }else{
            isInsideWalls = false;
        }
    }
}

EDIT: When i run this code, this error appears: 编辑:当我运行此代码时,出现此错误:

`Exception in thread "AWT-EventQueue-0" Exception in thread "Timer-0" java.lang.NullPointerException
    at java.awt.Rectangle.intersects(Unknown Source)
    at bombermangame.Game.intersectsBox(Game.java:122)
    at bombermangame.Game.walls(Game.java:147)
    at bombermangame.Game.paintComponent(Game.java:161)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)

The following loop below will need to be removed as it creates 63 copies of your wall grid. 下面的循环将需要删除,因为它会创建63个墙面网格副本。 What this means is that every single coordinate you specify will contains 63 rectangles. 这意味着您指定的每个单个坐标都将包含63个矩形。

 for (int i = 0; i < 63; i++) {

The reason removing the for loop will fix at least one of your problems is because when you remove the first rectangle, there are 62 rectangles left at the same coordinate. 删除for循环将至少解决您的一个问题的原因是,当删除第一个矩形时,在同一坐标处还剩下62个矩形。

All you will need to create your list of walls is the following: 创建墙壁列表所需要做的只是以下操作:

for (int wallsX = 0; wallsX < 750; wallsX += 95) {
      for (int wallsY = 0; wallsY < 750; wallsY += 95) {
           shapeList.add(new Rectangle(wallsX, wallsY, 95, 95));
           g.setColor(Color.blue);
           g.drawRect(wallsX, wallsY, 95, 95);
      }
}

Now once you apply shapeList.remove(0); 现在,一旦您应用shapeList.remove(0);

There should be no walls at coordinate (0,0) anymore. 坐标(0,0)处不再有墙。

There may be other errors in your code that I am unaware of though. 我的代码中可能还存在其他我不知道的错误。 I've only looked at your code that you've provided in your question. 我只看了您在问题中提供的代码。

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

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