2015-12-06 29 views
0

我想通過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(); 
+0

的'PathTransition'只是改變了'translateX'和節點的'translateY'性質正在轉移,所以你可以註冊一個監聽器與性能並在他們改變任何時候做任何你需要的事情。 –

+0

好的,thx,你能告訴我一個你是什麼意思的例子嗎? – Sparkoenig

+0

那麼,你可以發佈一些我可以使用的轉換示例代碼嗎?爲什麼你需要這個職位,例如? –

回答

0

PathTransition將通過操縱其translateXtranslateY特性移動的節點。 (A TranslateTransition以同樣的方式。)

很難明確地回答你的問題是你的代碼是如此不完整的,但如果projectilecollider在場景圖中相同的父,轉換projectile的初始座標和collider通過調用localToParent將給出父母的座標,包括翻譯。因此,您可以觀察translateXtranslateY屬性並使用該轉換來檢查衝突。如果他們有不同的父母,您可以使用localToScene來做同樣的事情,只需將它們轉換爲相對於場景的座標即可。

這是一個快速的SSCCE。使用左,右箭頭瞄準,空間拍攝:

import javafx.animation.Animation; 
import javafx.animation.TranslateTransition; 
import javafx.application.Application; 
import javafx.beans.binding.Bindings; 
import javafx.beans.binding.BooleanBinding; 
import javafx.geometry.Point2D; 
import javafx.scene.Scene; 
import javafx.scene.input.KeyCode; 
import javafx.scene.layout.Pane; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Circle; 
import javafx.scene.shape.Line; 
import javafx.scene.transform.Rotate; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class ShootingGame extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     final double width = 400 ; 
     final double height = 400 ; 

     final double targetRadius = 25 ; 
     final double projectileRadius = 5 ; 

     final double weaponLength = 25 ; 

     final double weaponX = width/2 ; 
     final double weaponStartY = height ; 
     final double weaponEndY = height - weaponLength ; 

     final double targetStartX = targetRadius ; 
     final double targetY = targetRadius * 2 ;; 

     Pane root = new Pane(); 
     Circle target = new Circle(targetStartX, targetY, targetRadius, Color.BLUE); 
     TranslateTransition targetMotion = new TranslateTransition(Duration.seconds(2), target); 
     targetMotion.setByX(350); 
     targetMotion.setAutoReverse(true); 
     targetMotion.setCycleCount(Animation.INDEFINITE); 
     targetMotion.play(); 

     Line weapon = new Line(weaponX, weaponStartY, weaponX, weaponEndY); 
     weapon.setStrokeWidth(5); 
     Rotate weaponRotation = new Rotate(0, weaponX, weaponStartY); 
     weapon.getTransforms().add(weaponRotation); 

     Scene scene = new Scene(root, width, height); 
     scene.setOnKeyPressed(e -> { 
      if (e.getCode() == KeyCode.LEFT) { 
       weaponRotation.setAngle(Math.max(-45, weaponRotation.getAngle() - 2)); 
      } 
      if (e.getCode() == KeyCode.RIGHT) { 
       weaponRotation.setAngle(Math.min(45, weaponRotation.getAngle() + 2)); 
      } 
      if (e.getCode() == KeyCode.SPACE) { 

       Point2D weaponEnd = weapon.localToParent(weaponX, weaponEndY); 

       Circle projectile = new Circle(weaponEnd.getX(), weaponEnd.getY(), projectileRadius); 

       TranslateTransition shot = new TranslateTransition(Duration.seconds(1), projectile); 
       shot.setByX(Math.tan(Math.toRadians(weaponRotation.getAngle())) * height); 
       shot.setByY(-height); 
       shot.setOnFinished(event -> root.getChildren().remove(projectile)); 

       BooleanBinding hit = Bindings.createBooleanBinding(() -> { 
        Point2D targetLocation = target.localToParent(targetStartX, targetY); 
        Point2D projectileLocation = projectile.localToParent(weaponEnd); 
        return (targetLocation.distance(projectileLocation) < targetRadius + projectileRadius) ; 
       }, projectile.translateXProperty(), projectile.translateYProperty()); 

       hit.addListener((obs, wasHit, isNowHit) -> { 
        if (isNowHit) { 
         System.out.println("Hit"); 
         root.getChildren().remove(projectile); 
         root.getChildren().remove(target); 
         targetMotion.stop(); 
         shot.stop(); 
        } 
       }); 

       root.getChildren().add(projectile); 
       shot.play(); 
      } 
     }); 

     root.getChildren().addAll(target, weapon); 

     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

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

謝謝!它爲我工作:) – Sparkoenig

相關問題