gpt4 book ai didi

JavaFx:运行时检查 PathTransition 的当前位置

转载 作者:行者123 更新时间:2023-11-30 07:44:32 25 4
gpt4 key购买 nike

我想获取圆 (javafx.scene.shape.Circle) 的当前位置 (x,y),当转换正在运行/发生时,我正在通过 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 将通过操作节点的 translateXtranslateY 属性来移动节点。 (TranslateTransition 的工作方式相同。)

很难明确地回答你的问题,因为你的代码是如此不完整,但是如果projectilecollider在场景图中具有相同的父级,则转换初始坐标通过调用 localToParent 来计算 projectilecollider 将给出父级中的坐标,包括平移。因此,您可以观察 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);
}
}

关于JavaFx:运行时检查 PathTransition 的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34113327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com