2015-10-13 4 views
0

Когда я запустил этот код, действие прекратится, когда щелкнуть мышью. Мяч перестанет действовать, когда я нажимаю кнопку мыши. как заставить мяч делать действие непрерывно, хотя я нажимаю кнопку мыши, чтобы добавить другие шары.javafx timeline и mouse event

import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Bounds; 
import javafx.scene.Scene; 
import javafx.scene.input.MouseButton; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class GamePractice extends Application { 

    public static Circle circle; 
    public static Pane canvas; 

    @Override 
    public void start(final Stage primaryStage) { 

     canvas = new Pane(); 
     final Scene scene = new Scene(canvas, 800, 600); 

     primaryStage.setTitle("Game"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     circle = new Circle(15, Color.BLUE); 
     circle.relocate(100, 100); 

     canvas.getChildren().addAll(circle); 


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

      double deltaX = (double)(Math.random()*10) + 3; 
      double deltaY = (double)(Math.random()*10) + 3; 
      @Override 
      public void handle(final ActionEvent t) { 
       circle.setLayoutX(circle.getLayoutX() + deltaX); 
       circle.setLayoutY(circle.getLayoutY() + deltaY); 

       final Bounds bounds = canvas.getBoundsInLocal(); 
       final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius()); 
       final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius()); 
       final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius()); 
       final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius()); 

       if (atRightBorder || atLeftBorder) { 
        deltaX *= -1; 
       } 
       if (atBottomBorder || atTopBorder) { 
        deltaY *= -1; 
       } 

      } 
     })); 

     scene.setOnMousePressed(new EventHandler<MouseEvent>() {   
      @Override   
      public void handle(MouseEvent event) { 
       if (event.getButton() == MouseButton.PRIMARY) { 
        if (!(canvas.getChildren().isEmpty())) { 
         canvas.getChildren().remove(0); 
        } 
       } 
       else { 
        int red = (int)(Math.random()*256); 
        int green = (int)(Math.random()*256); 
        int blue = (int)(Math.random()*256); 
        int x = (int)(Math.random()*801); 
        int y = (int)(Math.random()*601); 
        circle = new Circle(15, Color.rgb(red, green, blue)); 
        circle.relocate(x, y); 
        canvas.getChildren().addAll(circle); 
       } 
      }  
     }); 

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

    public static void main(final String[] args) { 
     launch(args); 
    } 
} 
+0

Вы можете разместить код, который будет компилировать? На самом деле непонятно, что это должно делать. (Вы сбросили обработчик события на сцену сто раз в секунду?) –

+0

Я его отредактировал. Теперь предыдущий мяч прекратит движение, когда я нажму справа, чтобы добавить новый мяч. Я хочу, чтобы все шары двигались. – IPO

+0

Уверен, потому что анимация обновляет только один круг, и вы заменяете круг, на который он ссылается. Вам нужно сохранить «Список ». Как в сторону, почему вы сделали поля статичными? –

ответ

0

Просто создайте анимацию для каждого круга:

import javafx.animation.KeyFrame; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.event.ActionEvent; 
import javafx.event.EventHandler; 
import javafx.geometry.Bounds; 
import javafx.scene.Scene; 
import javafx.scene.input.MouseButton; 
import javafx.scene.input.MouseEvent; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class GamePractice extends Application { 

    private Pane canvas; 

    @Override 
    public void start(final Stage primaryStage) { 

     canvas = new Pane(); 
     final Scene scene = new Scene(canvas, 800, 600); 

     primaryStage.setTitle("Game"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     addCircle(100, 100, Color.BLUE); 


     scene.setOnMousePressed(new EventHandler<MouseEvent>() {   
      @Override   
      public void handle(MouseEvent event) { 
       if (event.getButton() == MouseButton.PRIMARY) { 
        if (!(canvas.getChildren().isEmpty())) { 
         canvas.getChildren().remove(0); 
        } 
       } 
       else { 
        int red = (int)(Math.random()*256); 
        int green = (int)(Math.random()*256); 
        int blue = (int)(Math.random()*256); 
        int x = (int)(Math.random()*801); 
        int y = (int)(Math.random()*601); 
        addCircle(x, y, Color.rgb(red, green, blue)); 
       } 
      }  
     }); 

    } 

    private void addCircle(double x, double y, Color color) { 
     Circle circle = new Circle(15, color); 
     circle.relocate(x, y); 

     canvas.getChildren().addAll(circle); 


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

      double deltaX = (double)(Math.random()*10) + 3; 
      double deltaY = (double)(Math.random()*10) + 3; 


      @Override 
      public void handle(final ActionEvent t) { 
       circle.setLayoutX(circle.getLayoutX() + deltaX); 
       circle.setLayoutY(circle.getLayoutY() + deltaY); 

       final Bounds bounds = canvas.getBoundsInLocal(); 
       final boolean atRightBorder = circle.getLayoutX() >= (bounds.getMaxX() - circle.getRadius()); 
       final boolean atLeftBorder = circle.getLayoutX() <= (bounds.getMinX() + circle.getRadius()); 
       final boolean atBottomBorder = circle.getLayoutY() >= (bounds.getMaxY() - circle.getRadius()); 
       final boolean atTopBorder = circle.getLayoutY() <= (bounds.getMinY() + circle.getRadius()); 

       if (atRightBorder || atLeftBorder) { 
        deltaX *= -1; 
       } 
       if (atBottomBorder || atTopBorder) { 
        deltaY *= -1; 
       } 
      } 
     })); 

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

    } 

    public static void main(final String[] args) { 
     launch(args); 
    } 
} 
+0

Я вижу. Спасибо большое. – IPO