JavaFX 检查窗格中动画形状之间的碰撞 - 多重碰撞
JavaFX checking collision between animated shapes on pane - multiple collisions
我正在使用 JavaFX 制作一个简单的游戏,球从底部释放(通过按钮)后会在屏幕上弹跳。当球在窗格周围弹跳时,如果它击中 Rectangle
,它会将颜色更改为蓝色。
我正在尝试添加一个名为 checkBounds 的方法来跟踪球(Circle
)何时击中 Rectangle
。一旦球接触到矩形,分数应该增加 10。计分机制起作用,但它会随着球通过 Rectangle
的每一帧而继续增加,而不是仅增加一次它进入。 (例如,它应该只上升 10 一次,而不是在球通过的整个过程中继续上升 10)。我在我的时间线循环中调用 checkBounds()
3 次以检查每个循环迭代中的每个矩形。
checkBounds(ball, gameSquare);
checkBounds(ball, gameSquare2);
checkBounds(ball, gameSquare3);
我该如何解决这个逻辑错误?我尝试了几种不同的选择,但 none 似乎有效。
private void checkBounds(Shape block, Rectangle rect) {
boolean collisionDetected = false;
Shape intersect = Shape.intersect(block, rect);
if (intersect.getBoundsInLocal().getWidth() != -1) {
collisionDetected = true;
}
if (collisionDetected) {
score.setText(Integer.toString(currentScore.getCurrentValue()));
currentScore.incrementBy(10);
rect.setFill(Color.BLUE);
}
}
我相信你想要检测的是你的 collisionDetected
从 false
到 true
的状态变化。一种方法是将前一个值的状态作为每个碰撞对象的成员变量。为此,我们需要矩形的一些标识 id,因此您可能希望将 id 传递到 checkBounds
方法中:
private void checkBounds(Shape block, Rectangle rect, int id) {
我们还需要创建一个成员变量来存储状态:
private HashMap<Integer,Boolean> previousCollisionStateMap = new HashMap<>();
然后在您的 checkBounds
代码中,您可以修改条件以检查更改
Boolean prevState = previousCollisionStateMap.get(id);
if (prevState == null) { // this is just to initialize value
prevState = false;
previousCollisionStateMap.put(id,false);
}
if (!prevState && collisionDetected) {
score.setText(Integer.toString(currentScore.getCurrentValue()));
currentScore.incrementBy(10);
rect.setFill(Color.BLUE);
}
别忘了在最后更新状态
previousCollisionStateMap.put(id,collisionDetected);
我正在使用 JavaFX 制作一个简单的游戏,球从底部释放(通过按钮)后会在屏幕上弹跳。当球在窗格周围弹跳时,如果它击中 Rectangle
,它会将颜色更改为蓝色。
我正在尝试添加一个名为 checkBounds 的方法来跟踪球(Circle
)何时击中 Rectangle
。一旦球接触到矩形,分数应该增加 10。计分机制起作用,但它会随着球通过 Rectangle
的每一帧而继续增加,而不是仅增加一次它进入。 (例如,它应该只上升 10 一次,而不是在球通过的整个过程中继续上升 10)。我在我的时间线循环中调用 checkBounds()
3 次以检查每个循环迭代中的每个矩形。
checkBounds(ball, gameSquare);
checkBounds(ball, gameSquare2);
checkBounds(ball, gameSquare3);
我该如何解决这个逻辑错误?我尝试了几种不同的选择,但 none 似乎有效。
private void checkBounds(Shape block, Rectangle rect) {
boolean collisionDetected = false;
Shape intersect = Shape.intersect(block, rect);
if (intersect.getBoundsInLocal().getWidth() != -1) {
collisionDetected = true;
}
if (collisionDetected) {
score.setText(Integer.toString(currentScore.getCurrentValue()));
currentScore.incrementBy(10);
rect.setFill(Color.BLUE);
}
}
我相信你想要检测的是你的 collisionDetected
从 false
到 true
的状态变化。一种方法是将前一个值的状态作为每个碰撞对象的成员变量。为此,我们需要矩形的一些标识 id,因此您可能希望将 id 传递到 checkBounds
方法中:
private void checkBounds(Shape block, Rectangle rect, int id) {
我们还需要创建一个成员变量来存储状态:
private HashMap<Integer,Boolean> previousCollisionStateMap = new HashMap<>();
然后在您的 checkBounds
代码中,您可以修改条件以检查更改
Boolean prevState = previousCollisionStateMap.get(id);
if (prevState == null) { // this is just to initialize value
prevState = false;
previousCollisionStateMap.put(id,false);
}
if (!prevState && collisionDetected) {
score.setText(Integer.toString(currentScore.getCurrentValue()));
currentScore.incrementBy(10);
rect.setFill(Color.BLUE);
}
别忘了在最后更新状态
previousCollisionStateMap.put(id,collisionDetected);