如何防止Java FX中的形状重叠?

时间:2015-04-10 20:42:25

标签: java javafx collision-detection game-physics

如何防止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);

    });

1 个答案:

答案 0 :(得分:0)

如果它发生碰撞而你希望它停止,只是不要修改翻译值。

所有人都可以从你提供的作品中获得。