2016-07-30 12 views
1

Ich versuche, den PathTransition zu pausieren und starten Sie es mit der Tastaturtaste P. Und auch ich versuche, die Animationsgeschwindigkeit mit den UP/Down-Tasten zu erhöhen und zu verringern.onKeyPressed Funktion funktioniert nicht in JavaFX

Aber wenn ich den Code ausführen, scheinen diese Tasten nicht zu funktionieren. Was mache ich falsch?

package exercise_15_29; 

import javafx.animation.Animation; 
import javafx.animation.PathTransition; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.scene.shape.Polygon; 
import javafx.scene.input.KeyCode; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class Exercise_15_29 extends Application { 
    Group car = new Group(); 
    Circle wheel1 = new Circle(15,95,5); 
    Circle wheel2 = new Circle(35,95,5); 
    Polygon body1 = new Polygon(); 
    Rectangle body2 = new Rectangle(0.0,80.0,50,10); 
    Line path = new Line(0,90,500,90); 

    int speed = 4000; 
    boolean play = true; 

    @Override 
    public void start(Stage primaryStage) { 

     body1.getPoints().addAll(new Double[]{ 
      10.0, 80.0, 
      20.0, 70.0, 
      30.0, 70.0, 
      40.0, 80.0 
     }); 
     body1.setFill(Color.BLUE); 
     body2.setFill(Color.SKYBLUE); 
     path.setVisible (false); 

     car.getChildren().addAll(wheel1,wheel2,body1,body2); 

     PathTransition pt = new PathTransition(); 
     pt.setDuration(Duration.millis(speed)); 
     pt.setPath(path); 
     pt.setNode(car); 
     pt.setCycleCount(Timeline.INDEFINITE); 
     pt.setAutoReverse(false); 

     pt.play(); 

     Pane root = new Pane(); 
     root.getChildren().add(car); 
     root.getChildren().add(path); 

     Scene scene = new Scene(root, 500, 100); 

     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

     root.setOnKeyTyped(e -> { 
      if(e.getCode() == KeyCode.P) { 
       if(play == true) 
        pt.stop(); 
       else 
        pt.play(); 
       play = !play; 
      } 
     }); 

     root.setOnKeyPressed(e -> { 
      if(e.getCode() == KeyCode.UP) 
       pt.setDuration(Duration.millis(++speed)); 
      else if(e.getCode() == KeyCode.DOWN) 
       pt.setDuration(Duration.millis(--speed)); 
     }); 
    } 

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

} 
+1

Nicht sicher, warum, aber es scheint zu funktionieren, wenn Sie den Ereignis-Listener auf die 'Szene' anstelle der' Scheibe' setzen. Wahrscheinlich etwas mit dem Fokusmodell ... – Itai

Antwort

2

dieser Code funktioniert für mich:

import javafx.animation.PathTransition; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.scene.Group; 
import javafx.scene.Scene; 
import javafx.scene.input.KeyCode; 
import javafx.scene.input.KeyEvent; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.scene.shape.Polygon; 
import javafx.scene.shape.Rectangle; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class Exercise_15_29 extends Application { 
    Group car = new Group(); 
    Circle wheel1 = new Circle(15, 95, 5); 
    Circle wheel2 = new Circle(35, 95, 5); 
    Polygon body1 = new Polygon(); 
    Rectangle body2 = new Rectangle(0.0, 80.0, 50, 10); 
    Line path = new Line(0, 90, 500, 90); 

    int speed = 4000; 
    boolean play = true; 

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

    @Override 
    public void start(Stage primaryStage) { 

     body1.getPoints().addAll(new Double[]{ 
       10.0, 80.0, 
       20.0, 70.0, 
       30.0, 70.0, 
       40.0, 80.0 
     }); 
     body1.setFill(Color.BLUE); 
     body2.setFill(Color.SKYBLUE); 
     path.setVisible(false); 

     car.getChildren().addAll(wheel1, wheel2, body1, body2); 

     PathTransition pt = new PathTransition(); 
     pt.setDuration(Duration.millis(speed)); 
     pt.setPath(path); 
     pt.setNode(car); 
     pt.setCycleCount(Timeline.INDEFINITE); 
     pt.setAutoReverse(false); 

     pt.play(); 

     Pane root = new Pane(); 
     root.getChildren().add(car); 
     root.getChildren().add(path); 

     Scene scene = new Scene(root, 500, 100); 

     scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> { 
      System.out.println("P"); 
      if (e.getCode() == KeyCode.P) { 
       if (play) 
        pt.stop(); 
       else 
        pt.play(); 
       play = !play; 
      } 
     }); 
     primaryStage.setTitle("Hello World!"); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

//  root.addEventFilter(KeyEvent.KEY_PRESSED, e -> { 
//   if (e.getCode() == KeyCode.P) { 
//    if (play == true) 
//     pt.stop(); 
//    else 
//     pt.play(); 
//    play = !play; 
//   } 
//  }); 


     scene.setOnKeyPressed(e -> { 
      System.out.println("UP"); 
      if (e.getCode() == KeyCode.UP) 
       pt.setDuration(Duration.millis(++speed)); 
      else if (e.getCode() == KeyCode.DOWN) 
       pt.setDuration(Duration.millis(--speed)); 
     }); 
//  root.setOnKeyPressed(e -> { 
//   if(e.getCode() == KeyCode.UP) 
//    pt.setDuration(Duration.millis(++speed)); 
//   else if(e.getCode() == KeyCode.DOWN) 
//    pt.setDuration(Duration.millis(--speed)); 
//  }); 
    } 

} 

I KeyTyped Ereignis KEY_PRESSED geändert (ich dies empfehlen), und verwendet auch scene.addEventFilter statt root.setOnKeyPressed nach https://stackoverflow.com/a/24126049/3291867 und schließlich kann man nicht ändern Geschwindigkeit des Autos, Sie können die Dauer der Animation nach oder während des Spiels nicht ändern (wie ich weiß), können Sie AnimationTimer dafür verwenden.

+0

Warum können wir die Animationsdauer nicht ändern? Was passiert, wenn ich pausiere und dann die Geschwindigkeit ändere? Ich habe über 'AnimationTimer' gelesen. Gibt es einen einfacheren Weg? –

+0

Ich bin mir nicht sicher, aber etwas, das ich weiß, ist, dass Sie die Dauer beim Abspielen von Animationen nicht ändern können. @MohsinAnees – mojtab23