JavaFx Rectangle Array与球对象碰撞并删除

时间:2016-03-16 00:51:51

标签: arrays javafx collision geometry rectangles

我正在尝试制作像砖头破坏者一样的游戏,称为'dxBall' 但是当谈到矩形和球碰撞时,我的代码不起作用或者很可能我写错了。我正在创建一个矩形阵列,这很酷但是当它们之间发生碰撞时,球会穿过矩形砖的左侧或右侧,也无法从屏幕上移除。我该如何修复此代码?

public class Main extends Application {

    final static int Width=800;
    final static int Height=480;
    private double velocity=10;
    double dx=3;
    double dy=3;
    int radius = 10;
    int xx=400;
    int yy=240;

    //Method of Creating Bricks
    static Rectangle createBrick(int x, int y) {
        Rectangle brick = new Rectangle(x, y, 50, 20);
        brick.setFill(Color.RED);

        return brick;
    }
    //method for move the bar
    private void moveCircleOnKeyPress(Scene gamescene,Rectangle bar) {
        gamescene.setOnKeyPressed(new EventHandler <KeyEvent>() {
            @Override public void handle(KeyEvent event) {
                switch (event.getCode()) {
                    case RIGHT: bar.setX(bar.getX() + velocity); break;
                    case LEFT:  bar.setX(bar.getX() - velocity); break;
                }
            }
        });
    }



    @Override
    public void start(Stage primaryStage) throws Exception{




        Group root = new Group();
        VBox layoutMenu= new VBox(10);
        layoutMenu.setPrefWidth(100);
        Label Score = new Label();
        Score.setText("Score: ");
        Score.setLayoutX(720);
        Score.setLayoutY(5);

        //Array of Bricks
        Rectangle b [] = new Rectangle[45];
        int i=0;
        int x = 0;
        int y = 30;
        for(i=0, x = 0; x<=750; x=x+53,i=i+1 ){
            b[i] = createBrick(x,y);
            root.getChildren().add(b[i]);
            if(x==742){
                for( x = 0; x<=750; x=x+53,i=i+1 ) {
                    b[i] = createBrick(x,y+21);
                    root.getChildren().add(b[i]);
                    if(x==742){
                        for( x = 0; x<=750; x=x+53,i=i+1 ) {
                            b[i] = createBrick(x,y+42);
                            root.getChildren().add(b[i]);
                        }
                    }
                }
            }
        }

        //Declaring scenes
        Scene menu = new Scene(layoutMenu,Width,Height);
        Scene gamescene = new Scene(root,Width,Height);


        //Creating Buttons
        Button playGameButton= new Button("Play Game");
        Button scoresButton = new Button("Scores");
        Button exitButton = new Button("Exit");
        Button menuButton = new Button("Menu");

        //Creating Ball
        Circle ball = new Circle(radius,Color.DARKBLUE);
        ball.relocate(xx,yy);


        //Creating Bar
        Rectangle bar = new Rectangle(325,455,150,20);
        bar.setFill(Color.DARKGREEN);


        // Adding items to the layoutMenu and gameMenu and gamescene
        layoutMenu.getChildren().addAll(playGameButton,scoresButton,exitButton);
        root.getChildren().addAll(menuButton,bar,ball,Score);


        //Align buttons and bar
        layoutMenu.setAlignment(Pos.CENTER);
        playGameButton.setMinWidth(layoutMenu.getPrefWidth());
        scoresButton.setMinWidth(layoutMenu.getPrefWidth());
        exitButton.setMinWidth(layoutMenu.getPrefWidth());
        menuButton.setAlignment(Pos.TOP_RIGHT);

        //Events of buttons
        playGameButton.setOnAction(e -> primaryStage.setScene(gamescene));
        menuButton.setOnAction(e -> primaryStage.setScene(menu));

        layoutMenu.setStyle("-fx-background-color: #2C3539;");
        primaryStage.setTitle("dxBall");
        primaryStage.setScene(menu);

        moveCircleOnKeyPress(gamescene, bar);
        primaryStage.show();

        final Timeline loop = new Timeline(new KeyFrame(Duration.millis(10), new EventHandler<ActionEvent>() {



            @Override
            public void handle(final ActionEvent t) {
                ball.setLayoutX(ball.getLayoutX() + dx);
                ball.setLayoutY(ball.getLayoutY() + dy);

                //Collision for walls
                final boolean atRightBorder = ball.getLayoutX() >= (800 - radius);
                final boolean atLeftBorder = ball.getLayoutX() <= (0 + radius);
                final boolean atBottomBorder = ball.getLayoutY() >= (480 - radius);
                final boolean atTopBorder = ball.getLayoutY() <= (0 + radius+30);

                if (atRightBorder || atLeftBorder) {
                    dx *= -1;
                }
                if (atBottomBorder || atTopBorder) {
                    dy *= -1;
                }

                //Collision for brick bar

                if(ball.getBoundsInParent().intersects(bar.getLayoutBounds())){
                    Shape intersect = Shape.intersect(ball, bar);
                    if(intersect.getBoundsInLocal().getWidth() != -1){
                        // Collision handling here
                    }
                    dy*=-1;
                }
                //Bar must stay in scene
                final boolean atRight = bar.getX() >= (800 - 150);
                final boolean atLeft = bar.getX() <= (0);

                if(atRight){
                    bar.setX(650);
                }
                else if(atLeft){
                    bar.setX(0);
                }
            }
        }));

        loop.setCycleCount(Timeline.INDEFINITE);
        loop.play();
    }



    public static void main(String[] args) {

        launch(args);
    }
}

1 个答案:

答案 0 :(得分:0)

你没有砖块的交叉码,i。即像这样的东西:

for( Rectangle brick: b) {

  if( brick == null)
    continue;

  if( ball.getBoundsInParent().intersects(brick.getBoundsInParent())) {
    ...
  }

}

顺便说一句,也应该针对getBoundsInParent而不是getLayoutBounds检查该栏。