JAVAFX游戏碰撞

时间:2013-12-06 10:17:00

标签: javafx collision

我正在JAVAFX做一个学校项目,我无法弄清楚如何与我的2个物体(子弹和敌人坦克)发生碰撞。有人能告诉我正确的方法吗?我正在尝试超过3周...谷歌一切,但仍然没有工作。

public class TankGame extends Application {

private final static int WIDTH = 800;
private final static int HEIGHT = 600;    
private final static Image BACKGROUND_IMAGE = new       Image(TankGame.class.getResource("imgs/Tank_back.png").toString()); 
private final static Image PATRONA = new Image(TankGame.class.getResource("imgs/Tank_patrona.png").toString());
private Animation modelAnimacePatrony;
private Group patrona;
private double smerStrelyX, smerStrelyY;
private Otaceni otaceni = new Otaceni();
private TankHrac tankHrac = new TankHrac();
private TankProtivnik tankProtivnik = new TankProtivnik();

@Override
public void start(Stage primaryStage) {


    final ImageView background = new ImageView(BACKGROUND_IMAGE);
    final ImageView bullet = new ImageView(PATRONA);
    patrona = new Group(bullet);
    final Group root = new Group(background, tankHrac, tankProtivnik, patrona);//deti

    patrona.setVisible(false);

    Scene scene = new Scene(root, WIDTH, HEIGHT); //okno

    tankHrac.setTranslateX(50);//defaultni vyskyt modelu
    tankHrac.setTranslateY(50);//defaultni vyskyt modelu
    tankProtivnik.setTranslateX(350);//defaultni vyskyt modeluProtivnika
    tankProtivnik.setTranslateY(150);//defaultni vyskyt modeluProtivnika

    smerStrelyX = tankHrac.getTranslateX();
    smerStrelyY = tankHrac.getTranslateY()-250;

     scene.setOnKeyPressed(new EventHandler<KeyEvent>() {

        @Override
        public void handle(KeyEvent ke) {
            /**
             * Shooting
             */
            if( ke.getCode() == KeyCode.SPACE ) { 
                if(!patrona.isVisible()){
                //patrona.setVisible(true);
                shooting(smerStrelyX,smerStrelyY, tankHrac);
                }
            }
        }
    });

    primaryStage.setTitle("Tank 1.0");
    primaryStage.setScene(scene);
    primaryStage.show();
    primaryStage.setResizable(false);
}

public void shooting(double smerStrelyX, double smerStrelyY, TankHrac jakyModelTanku){
    patrona.setVisible(true);
    modelAnimacePatrony = TranslateTransitionBuilder.create()
            .node(patrona)
            .fromX(jakyModelTanku.getTranslateX()+30)
            .toX(smerStrelyX+30)
            .fromY(jakyModelTanku.getTranslateY()+30)
            .toY(smerStrelyY+30)
            .duration(Duration.seconds(1))
            .onFinished(new EventHandler<ActionEvent>(){
                @Override
                public void handle(ActionEvent t){

                    modelAnimacePatrony.stop();
                    patrona.setVisible(false);
                }
            })
            .build();
    modelAnimacePatrony.play();

} 

以下是所有源文件:https://www.dropbox.com/sh/1iq98jtxh8m06tt/7Y9LQSjfYs

0 个答案:

没有答案