Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.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)_Java_Android_Collision Detection - Fatal编程技术网

破砖游戏中的球会破坏整条线而不触碰它们(Java)

破砖游戏中的球会破坏整条线而不触碰它们(Java),java,android,collision-detection,Java,Android,Collision Detection,我一直在做一个砖块破坏游戏的乐趣,但我遇到了一个奇怪的错误,在执行程序。当游戏开始时,球开始远离砖块运动。然而,它几乎立即开始破坏它们,从最右边的砖开始,从上一排开始,继续从右到左破坏它们。然后它进入第二排,从左到右摧毁它们。在整个过程中,在屏幕上,球远离任何砖块 我想我把问题精确地定位到了下面的代码中。在这里,我创建了一个围绕球的矩形和一个围绕砖块的矩形,如果它们相交,我在另一个名为Map Generator的类中调用方法setBrickValue,该类将砖块的值设置为0,这意味着它不存在 方

我一直在做一个砖块破坏游戏的乐趣,但我遇到了一个奇怪的错误,在执行程序。当游戏开始时,球开始远离砖块运动。然而,它几乎立即开始破坏它们,从最右边的砖开始,从上一排开始,继续从右到左破坏它们。然后它进入第二排,从左到右摧毁它们。在整个过程中,在屏幕上,球远离任何砖块

我想我把问题精确地定位到了下面的代码中。在这里,我创建了一个围绕球的矩形和一个围绕砖块的矩形,如果它们相交,我在另一个名为
Map Generator
的类中调用方法
setBrickValue
,该类将砖块的值设置为0,这意味着它不存在

方法如下:

    public void actionPerformed(ActionEvent arg0) {
            timer.start();
            //makes sure the ball collides with the paddle
            if(play){
                if( new Rectangle( ballPositionX, ballPositionY, 20, 20)
                        .intersects( new Rectangle( player, 550, 100, 8 ) ) ){
                    ballDirY = - ballDirY;
                }
                // beginning of the code with an error in it, the rectangle around the ball
                //is intersecting incorrectly the rectangle of the brick
                A:for(int i = 0; i < MGobj.map.length; i++){
                    for(int j = 0; j < MGobj.map[0].length; j++){
                        if(MGobj.map [i] [j] > 0){
                            int brickX = i * MGobj.brickWidth + 80;
                            int brickY = j * MGobj.brickHeight + 50;
                            int brickWidth = MGobj.brickWidth;
                            int brickHeight = MGobj.brickHeight;

                            //create the rectangle around the brick
                            Rectangle rect = new Rectangle( brickX, brickY,
                                    brickWidth, brickHeight );

                            //create the rectangle around the ball, so we can detect the intersection
                            Rectangle ballRect = new Rectangle( ballPositionX, ballPositionY, 20, 20 );

                            Rectangle brickRect = rect;
                            //when intersecting, delete the brick off the map
                            if( ballRect.intersects( brickRect )){
                                MGobj.setBrickValue( 0, i, j );
                                totalBricks--;
                                score += 5;
                                //
                                if( ballPositionX + 19 >= brickRect.x || 
                                         ballPositionX + 1  <=  brickRect.x + brickRect.width  ){
                                    ballDirX = - ballDirX;
                                }else{
                                    ballDirY = - ballDirY;
                                }
                                //creates a label that says when this code executes, get out of the 
                                //outer loop
                                break A;
                            }
                         }
                      }
                    }
                //end of the code with an error in it


                //makes sure the ball is moving
                ballPositionX += ballDirX;
                ballPositionY += ballDirY;

                //makes sure the ball isn't going beyond the borders
                if(ballPositionX < 0){
                    ballDirX = -ballDirX;
                }
                if(ballPositionY < 0){
                    ballDirY = -ballDirY;
                }
                //think this code doesn't work
                if(ballPositionX > 670){
                    ballDirX = -ballDirX;
                }
            }

            repaint();

        }
感谢所有的帮助。先谢谢你


PS:如果有人想看到整个代码(400行),只要告诉我,我会上传它。

你有没有想过,不要使用
intersect
方法——只需将球的位置映射到选中的地图上的砖块坐标?这将消除遍历所有映射的需要,您只需检查在balls位置的映射中是否有一块砖块,这将使调试更容易,您只需启动调试器并逐步完成此循环,观察变量,了解代码实际如何工作。像
intballmappx=(ballX-80)/MGobj.brickWidth
我将尝试这样做。我想我可以在网上找到更多关于如何最好地执行此方法的教程。谢谢!:)我通过修复这4行代码解决了我的问题:
intbrickx=j*MGobj.brickWidth+80;int brickY=i*MGobj.brickHeight+50
如果(ballPositionX+19=brickRect.x+brickRect.width){
    public void setBrickValue(int value, int row, int col){     
          map [row] [col] = value;  
        }