gpt4 book ai didi

java - 不断检查子弹是否触及节点

转载 作者:搜寻专家 更新时间:2023-11-01 02:59:37 26 4
gpt4 key购买 nike

我有一个非常简单的程序,您可以在其中使用WASDspace 键进行射击。所有的射击和移动动画都有效,但我不确定如何实现一个程序不断检查子弹是否触及节点(例如圆圈)的系统。

我在想我可以用一个ArrayList 来存储所有的子弹,然后使用一个TimerTask 来检查子弹是否碰到了一个节点;但我觉得这会减慢程序速度,而且子弹可能会在 TimerTask 等待再次运行时通过它们。

任何建议都会有所帮助。

代码:Pastebin

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.animation.*;
import javafx.util.Duration;
public class functionalShooter extends Application {
private String currentDirection = "W";
public static void main(String[] args){ launch(args); }
@Override public void start(Stage stage) throws Exception{
Pane root = new Pane();
Scene scene = new Scene(root, 600, 400);
stage.setScene(scene);
stage.setResizable(false);
stage.show();

Circle player = new Circle(10);
player.setLayoutX(300);
player.setLayoutY(200);

Circle other = new Circle(100, 100, 10);

root.getChildren().addAll(player, other);

//key events
scene.setOnKeyPressed(event -> {
switch(event.getCode()){
case A:
player.setLayoutX(player.getLayoutX()-10);
currentDirection = "A";
break;
case D:
player.setLayoutX(player.getLayoutX()+10);
currentDirection = "D";
break;
case W:
player.setLayoutY(player.getLayoutY()-10);
currentDirection = "W";
break;
case S:
player.setLayoutY(player.getLayoutY()+10);
currentDirection = "S";
break;
case SPACE:
if (currentDirection.equals("D")){
Circle newCircle = new Circle(player.getLayoutX()+10, player.getLayoutY(), 5);
root.getChildren().add(newCircle);
shoot(newCircle);
}
else if (currentDirection.equals("A")){
Circle newCircle = new Circle(player.getLayoutX()-10, player.getLayoutY(), 5);
root.getChildren().add(newCircle);
shoot(newCircle);
}
else if (currentDirection.equals("S")){
Circle newCircle = new Circle(player.getLayoutX(), player.getLayoutY()+10, 5);
root.getChildren().add(newCircle);
shoot(newCircle);
}
else {
Circle newCircle = new Circle(player.getLayoutX(), player.getLayoutY()-10, 5);
root.getChildren().add(newCircle);
shoot(newCircle);
}
break;
}
});
}

private void shoot(Circle bullet){
Timeline timeline = new Timeline();
if (currentDirection.equals("D")){
KeyValue start = new KeyValue(bullet.translateXProperty(), 0);
KeyValue end = new KeyValue(bullet.translateXProperty(), 800);
KeyFrame startF = new KeyFrame(Duration.ZERO, start);
KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
timeline.getKeyFrames().addAll(startF, endF);
}
else if (currentDirection.equals("A")){
KeyValue start = new KeyValue(bullet.translateXProperty(), 0);
KeyValue end = new KeyValue(bullet.translateXProperty(), -800);
KeyFrame startF = new KeyFrame(Duration.ZERO, start);
KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
timeline.getKeyFrames().addAll(startF, endF);
}
else if (currentDirection.equals("S")){
KeyValue start = new KeyValue(bullet.translateYProperty(), 0);
KeyValue end = new KeyValue(bullet.translateYProperty(), 800);
KeyFrame startF = new KeyFrame(Duration.ZERO, start);
KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
timeline.getKeyFrames().addAll(startF, endF);
}
else{
KeyValue start = new KeyValue(bullet.translateYProperty(), 0);
KeyValue end = new KeyValue(bullet.translateYProperty(), -800);
KeyFrame startF = new KeyFrame(Duration.ZERO, start);
KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
timeline.getKeyFrames().addAll(startF, endF);
}
timeline.setAutoReverse(false);
timeline.setCycleCount(1);
timeline.play();
}
}

最佳答案

您可以使用 Shape.intersect() 检查 bullet 是否与 other 相交在自定义中 Interpolator提供给每个相关的KeyValue .下面的片段向 shoot() 添加了一个 Interpolator,但是每个方向都需要一个相同的插值器。实现是线性的,简单地返回 t 不变,但你也可以看看 this抛物线插值器。我还使 other 成为一个类变量,shoot() 可以访问它。我毫不迟疑地向空中发射了一打子弹。请注意,您不需要 start 值:“一个将是 synthesized,使用当前的目标值”播放动画。

private Circle other = new Circle(100, 100, 10);

else {
KeyValue end = new KeyValue(bullet.translateYProperty(), -800, new Interpolator() {
@Override
protected double curve(double t) {
if (!Shape.intersect(bullet, other).getBoundsInLocal().isEmpty()) {
System.out.println("Intersection");
}
return t;
}
});
KeyFrame endF = new KeyFrame(Duration.seconds(10), end);
timeline.getKeyFrames().addAll(endF);
}

关于java - 不断检查子弹是否触及节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39185306/

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