检查Java / Android对象列表上的冲突的有效方法

时间:2014-10-07 23:18:20

标签: java android performance collision

所以,我目前正在我的移动开发课程中开发Android游戏,我想知道是否有一种更有效的方法来检查碰撞,而不是我现在拥有的。现在我有三个列表,每个列表包含一排砖(总共3行,长度相等)。每次调用onDraw()方法时,我都会为每个列表中的每个砖调用checkCollision()(取决于几个条件[请参阅代码])。它工作正常,但我觉得这不是最有效的方法,我不想使用Rects。任何人都可以提出建议吗?这是我的代码:

在onDraw()中调用它:

    public void checkBrickCollision()
    {
        // Row 1
        // Only checks this collision if the first two rows are missing
        // elements (meaning the player has hit those elements)
        // and if this list is not empty
        if (brickListThree.size() < 6 
                && brickListTwo.size() < 6)
        {
            if (!brickListOne.isEmpty())
            {
                for (Brick b1 : brickListOne)
                {
                    if (b1.CheckYCollision(pongX, pongY, pong, pongUp, pongDown))
                    {
                        brickListOne.remove(b1);
                        ySpeed *= -1;
                        break;
                    }

                    if (b1.CheckXCollision(pongX, pongY, pong, pongRight, pongLeft))
                    {
                        brickListOne.remove(b1);
                        xSpeed *= -1;
                        break;
                    }
                }
            }
        }

        // Row 2
        // Only checks this collision if elements have been 
        // hit in row 3 and list is not empty
        if (brickListThree.size() < 6)
        {
            if (!brickListTwo.isEmpty())
            {
                for (Brick b2 : brickListTwo)
                {
                    if (b2.CheckYCollision(pongX, pongY, pong, pongUp, pongDown))
                    {
                        brickListTwo.remove(b2);
                        ySpeed *= -1;
                        break;
                    }

                    if (b2.CheckXCollision(pongX, pongY, pong, pongRight, pongLeft))
                    {
                        brickListTwo.remove(b2);
                        xSpeed *= -1;
                        break;
                    }
                }
            }
        }

        // Row 3
        // This collision is always checked as
        // as long as list is not empty
        for (Brick b3 : brickListThree)
        {
            if (!brickListThree.isEmpty())
            {
                if (b3.CheckYCollision(pongX, pongY, pong, pongUp, pongDown))
                {
                    brickListThree.remove(b3);
                    ySpeed *= -1;
                    break;
                }

                if (b3.CheckXCollision(pongX, pongY, pong, pongRight, pongLeft))
                {
                    brickListThree.remove(b3);
                    xSpeed *= -1;
                    break;
                }
            }
        }
    }

砖类函数

    public boolean CheckXCollision(int ballX, int ballY, Bitmap ballImg,
            boolean ballRight, boolean ballLeft)
    {               
        // If ball hits the left side of a brick
        if(ballY + ballImg.getHeight() >= brickY 
                && ballY <= brickY + brickH 
                && ballX + ballImg.getWidth() >= brickX - 2 
                && ballX + ballImg.getWidth() <= brickX + 2
                && ballRight)
        {
            this.isVisible = false;
            return true;
        }

        // If ball hits the right side of a brick
        else if(ballY + ballImg.getHeight() >= brickY 
                && ballY <= brickY + brickH 
                && ballX <= (brickX + brickW) + 2 
                && ballX >= brickX + brickW - 2
                && ballLeft)
        {
            this.isVisible = false; 
            return true;
        }

        return false;               
    }

    public boolean CheckYCollision(int ballX, int ballY, Bitmap ballImg,
            boolean ballUp, boolean ballDown)
    {       
        // If ball hits the top of a brick
        if (ballX + ballImg.getWidth() >= brickX 
                && ballX <= brickX + brickW
                && ballY + ballImg.getHeight() >= brickY - 2 
                && ballY + ballImg.getHeight() <= brickY + 2
                && ballDown)
        {
            this.isVisible = false;
            return true;
        }

        // If ball hits the bottom of a brick
        else if (ballX + ballImg.getWidth() >= brickX 
                && ballX <= brickX + brickW
                && ballY <= (brickY + brickH) + 2 
                && ballY >= brickY + brickH - 2
                && ballUp)
        {
            this.isVisible = false;
            return true;
        }

        return false;
    }

0 个答案:

没有答案