检查Java/Android对象列表冲突的有效方法
因此,我目前正在我的移动开发课程中为Android开发一款游戏,我想知道是否有比我目前拥有的更有效的方法来检查冲突。现在我有三个列表,每个列表包含一行砖块(总共3行,长度相等)。每次调用onDraw()方法时,我都会为每个列表中的每个块调用checkCollision()(取决于一些条件[参见代码])。它工作得很好,但我觉得这不是最有效的方法,我不想使用RECT。有人能提个建议吗?这是我的密码: 这在onDraw()中调用:检查Java/Android对象列表冲突的有效方法,java,android,performance,collision,Java,Android,Performance,Collision,因此,我目前正在我的移动开发课程中为Android开发一款游戏,我想知道是否有比我目前拥有的更有效的方法来检查冲突。现在我有三个列表,每个列表包含一行砖块(总共3行,长度相等)。每次调用onDraw()方法时,我都会为每个列表中的每个块调用checkCollision()(取决于一些条件[参见代码])。它工作得很好,但我觉得这不是最有效的方法,我不想使用RECT。有人能提个建议吗?这是我的密码: 这在onDraw()中调用: public void checkBrickCollision() {
public void checkBrickCollision()
{
//第1行
//仅在缺少前两行时检查此冲突
//元素(意味着玩家已经命中了这些元素)
//如果这个列表不是空的
如果(砖砌三个尺寸()<6
&&砌砖工2.尺寸()<6)
{
如果(!brickListOne.isEmpty())
{
用于(砖b1:砖砌体)
{
如果(b1.检查集合(pongX、pongY、pong、pongUp、pongDown))
{
砖砌体。拆除(b1);
y速度*=-1;
打破
}
如果(b1.检查集合(pongX、pongY、pong、pongRight、pongLeft))
{
砖砌体。拆除(b1);
xSpeed*=-1;
打破
}
}
}
}
//第2排
//仅当元素已被删除时才检查此冲突
//点击第3行,列表不为空
如果(brickListThree.size()<6)
{
如果(!bricklistwo.isEmpty())
{
用于(砖b2:砌砖厂2)
{
如果(b2.检查集合(pongX、pongY、pong、pongUp、pongDown))
{
砌砖工2.拆除(b2);
y速度*=-1;
打破
}
如果(b2.检查集合(pongX、pongY、pong、pongRight、pongLeft))
{
砌砖工2.拆除(b2);
xSpeed*=-1;
打破
}
}
}
}
//第3排
//此碰撞始终按以下方式进行检查:
//只要列表不是空的
用于(砖b3:砖砌工三)
{
如果(!brickListThree.isEmpty())
{
如果(b3.检查集合(pongX、pongY、pong、pongUp、pongDown))
{
砌砖工3.拆除(b3);
y速度*=-1;
打破
}
如果(b3.检查集合(pongX、pongY、pong、pongRight、pongLeft))
{
砌砖工3.拆除(b3);
xSpeed*=-1;
打破
}
}
}
}
砖块类函数
public boolean CheckXCollision(int-ballX、int-ballY、Bitmap-ballImg、,
布尔ballRight,布尔ballLeft)
{
//如果球击中了砖块的左侧
如果(ballY+ballImg.getHeight()>=brickY
&&巴利=砖x-2
&&ballX+ballImg.getWidth()=砖
&&巴利=砖
&&ballX=砖砌-2
&&ballY+ballImg.getHeight()=brickX
&&鲍尔克斯
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;
}