แปลตำแหน่งการรีเซ็ตการเปลี่ยนแปลงใน javaFX
ฉันพยายามย้ายวงกลมจาก (100,200) เป็น (400,200) และหลังจากหนึ่งรอบวงกลมควรเริ่มเคลื่อนที่จาก (100,100) ถึง (200,100) และทำซ้ำการเคลื่อนไหวนั้น หลังจากรอบแรกฉันรีเซ็ตตำแหน่งของวงกลมโดยใช้ circle.setCenterX (100) และ circle.setCenterY (100) อย่างไรก็ตามสิ่งนี้ไม่ได้สะท้อนให้เห็นในภาพเคลื่อนไหว วงกลมจะรีเซ็ตเป็น (400,100) และเคลื่อนที่ไปข้างหน้าในทิศทาง X แทนการเคลื่อนไหวซ้ำ ฉันเพิ่งเริ่มใช้ javaFX ความช่วยเหลือใด ๆ จะได้รับการชื่นชม
import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
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 Test extends Application
{
public static void main(String[] args)
{
launch(args);
}
final double lambda = 0.1; // pixel per millisecond
double posX = 100;
double posY = 200;
double time = 0;
double velocityX = 1*lambda;
double velocityY = 0*lambda;
Circle circle = new Circle(posX, posY, 20, Color.AQUA);
Circle ref1 = new Circle(100, 200, 5, Color.CADETBLUE);
Circle ref2 = new Circle(400, 200, 5, Color.CADETBLUE);
Circle ref3 = new Circle(100, 100, 5, Color.CADETBLUE);
@Override
public void start(Stage stage) throws Exception
{
Pane pane = new Pane();
pane.getChildren().addAll(circle, ref1, ref2, ref3);
BorderPane root = new BorderPane();
root.setCenter(pane);
root.setStyle("-fx-background-color: #29353B");
double WIDTH = 800;
double HEIGHT = 600;
Scene scene = new Scene(root, WIDTH, HEIGHT);
stage.setScene(scene);
stage.show();
move(3000);
}
public void move(double dt) // dt in milliseconds
{
System.out.println(circle.getCenterX()+", "+circle.getCenterY());
TranslateTransition translateTransition = new TranslateTransition(Duration.millis(dt), circle);
//translateTransition.setInterpolator(Interpolator.LINEAR);
translateTransition.setByX(this.velocityX*dt);
translateTransition.setByY(this.velocityY*dt);
translateTransition.setCycleCount(1);
translateTransition.play();
translateTransition.setOnFinished(actionEvent -> { updatePos(dt); move(2000); });
}
public void updatePos(double dt)
{
//this.posX += this.velocityX*dt;
//this.posY += this.velocityY*dt;
this.posX = 100;
this.posY = 100;
circle.setCenterX(this.posX);
circle.setCenterY(this.posY);
}
}
คำตอบ
TranslateTransition
ปรับเปลี่ยนtranslateX
และtranslateY
คุณสมบัติไม่centerX
และcenterY
คุณสมบัติ หากคุณแก้ไขคุณสมบัติcenterX
และcenterY
เมื่อภาพเคลื่อนไหวเสร็จสมบูรณ์คุณควรรีเซ็ตtranslateX
และtranslateY
เป็น 0 เพื่อให้วงกลมปรากฏที่พิกัดเหล่านั้น:
public void updatePos(double dt) {
//this.posX += this.velocityX*dt;
//this.posY += this.velocityY*dt;
this.posX = 100;
this.posY = 100;
circle.setCenterX(this.posX);
circle.setCenterY(this.posY);
circle.setTranslateX(0);
circle.setTranslateY(0);
}
หรือคุณสามารถใช้ a Timeline
แทนTranslateTransition
เพื่อจัดการคุณสมบัติcenterX
และโดยตรงcenterY
ในแอนิเมชั่น:
public void move(double dt) /* dt in milliseconds */ {
System.out.println(circle.getCenterX() + ", " + circle.getCenterY());
double targetX = circle.getCenterX() + this.velocityX * dt;
double targetY = circle.getCenterY() + this.velocityY * dt;
Timeline timeline = new Timeline(new KeyFrame(Duration.millis(dt),
new KeyValue(circle.centerXProperty(), targetX),
new KeyValue(circle.centerYProperty(), targetY)));
timeline.setOnFinished(actionEvent -> {
updatePos(dt);
move(2000);
});
timeline.play();
}
public void updatePos(double dt) {
this.posX = 100;
this.posY = 100;
circle.setCenterX(this.posX);
circle.setCenterY(this.posY);
}