如何防止Java FX中的形状重叠?
我已经进行了碰撞检测,但我不知道下一步该做什么..
//THIS IS MY COLLISION DETECTION
public void checkCollision(Characters a, Characters b, Characters c, Characters d)
{
if(a.getBoundsInParent().intersects(b.getBoundsInParent()))
{
info.setText("COLLISION DETECTED TO ENEMY 1");
}else if(a.getBoundsInParent().intersects(c.getBoundsInParent())){
info.setText("COLLISION DETECTED TO ENEMY 2");
}else if(a.getBoundsInParent().intersects(d.getBoundsInParent())){
info.setText("COLLISION DETECTED TO ENEMY 3");
}else{
info.setText("");
}
}
//AND THIS IS MY EVENT ON KEY PRESSED
scene.setOnKeyPressed(e -> {
if(e.getCode() == KeyCode.UP){
player.setTranslateY(player.getTranslateY() - 10);
}else if(e.getCode() == KeyCode.DOWN){
player.setTranslateY(player.getTranslateY() + 10);
}else if(e.getCode() == KeyCode.LEFT){
player.setTranslateX(player.getTranslateX() - 10);
}else if(e.getCode() == KeyCode.RIGHT){
player.setTranslateX(player.getTranslateX() + 10);
}
checkCollision(player, enemy, enemy2, enemy3);
});
答案 0 :(得分:0)
如果它发生碰撞而你希望它停止,只是不要修改翻译值。
所有人都可以从你提供的作品中获得。