2015-10-13 40 views
0

当我运行这段代码时,当鼠标点击时动作会停止。 当我点击鼠标时球会停止动作。尽管我点击鼠标来添加其他球,但我如何让球继续执行动作。javafx时间轴和鼠标事件

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

你能后的代码进行编译?这是真的不清楚这应该做什么。 (您正在每秒重置场景中的事件处理程序100次?) –

+0

我已编辑它。现在,当我点击右键添加新球时,先前的球会停止运动。我想要所有的球移动。 – IPO

+0

当然,因为动画只更新一个圆圈,并且替换了它所引用的圆圈。你需要保留一个'List '。顺便说一句,为什么你让这些字段是静态的? –

回答

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