我想通過PathTransition獲取Circle(javafx.scene.shape.Circle)的當前位置(x,y),而轉換正在運行/正在發生。JavaFx:在運行時檢查PathTransition的當前位置
所以我需要某種任務,每隔50毫秒檢查一次圓的位置(例如)。
我也試過這個解決方案Current circle position of javafx transition這是在Stack Overflow上建議的,但我似乎沒有爲我工作。
Circle projectile = new Circle(Playground.PROJECTILE_SIZE, Playground.PROJECTILE_COLOR);
root.getChildren().add(projectile);
double duration = distance/Playground.PROJECTILE_SPEED;
double xOff = (0.5-Math.random())*Playground.WEAPON_OFFSET;
double yOff = (0.5-Math.random())*Playground.WEAPON_OFFSET;
Line shotLine = new Line(player.getCurrentX(), player.getCurrentY(), aimLine.getEndX() + xOff, aimLine.getEndY() + yOff);
shotLine.setEndX(shotLine.getEndX() + (Math.random()*Playground.WEAPON_OFFSET));
PathTransition pt = new PathTransition(Duration.seconds(duration), shotLine, projectile);
// Linear movement for linear speed
pt.setInterpolator(Interpolator.LINEAR);
pt.setOnFinished(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
// Remove bullet after hit/expiration
projectile.setVisible(false);
root.getChildren().remove(projectile);
}
});
projectile.translateXProperty().addListener(new ChangeListener<Number>() {
@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
double x = collider.getTranslateX() - projectile.getTranslateX();
double y = collider.getTranslateY() - projectile.getTranslateY();
double distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
System.out.println("Distance: "+ distance);
if (distance < 50) {
System.out.println("hit");
}
}
});
pt.play();
的'PathTransition'只是改變了'translateX'和節點的'translateY'性質正在轉移,所以你可以註冊一個監聽器與性能並在他們改變任何時候做任何你需要的事情。 –
好的,thx,你能告訴我一個你是什麼意思的例子嗎? – Sparkoenig
那麼,你可以發佈一些我可以使用的轉換示例代碼嗎?爲什麼你需要這個職位,例如? –