【发布时间】:2013-12-24 09:07:13
【问题描述】:
我现在的代码是
private boolean checkHit2(int row, int col, Ball ball) {
// Check for a hit from below
if (ball.getTop() <= this.getBrickBottom( row) &&
ball.getTop() >= this.getBrickTop(row) &&
ball.getRight() >= this.getBrickLeft(col) &&
ball.getLeft() <= this.getBrickRight(col))
return true;
// Check for a hit from up
if(ball.getBottom() >= this.getBrickTop(row) &&
ball.getBottom() <= this.getBrickBottom(row) &&
ball.getRight() >= this.getBrickLeft(col) &&
ball.getLeft() <= this.getBrickRight(col))
return true;
// No hit
return false;
}
if(gameLevel.updateIfHit2(ballArr[i])){
if(ballArr[i].isBounceUp())
ballArr[i].down();
else
ballArr[i].bounceUp();
}
大多数情况下哪个效果很好。问题是有时它会检测到同一个砖块上的不止一次命中,因为代码“checkHit2”中的条件仍然成立(真)。 (每块砖需要超过 1 次击打才能消失,有时在击打砖块后它会继续击打而不是改变方向(例如,上翻而不是下翻)。
我也试过这个代码:
private boolean checkHit(int row, int col, Ball ball) {
// Check for a hit from below
if (ball.getTop() == this.getBrickBottom( row) &&
// ball.getTop() >= this.getBrickTop(row) &&
ball.getRight() >= this.getBrickLeft(col) &&
ball.getLeft() <= this.getBrickRight(col))
return true;
// Check for a hit from up
if(ball.getBottom() == this.getBrickTop(row) &&
// ball.getBottom() <= this.getBrickBottom(row) &&
ball.getRight() >= this.getBrickLeft(col) &&
ball.getLeft() <= this.getBrickRight(col))
return true;
// No hit
return false;
}
但这也有问题
【问题讨论】:
标签: java android collision-detection collision