JAVA FX-时间轴动画(在动画期间查找特定的x、y点)
在这个程序中,当for循环中的x==600时,我试图使rect变为红色。基本上,for循环的运行速度比屏幕上的动画快。矩形在实际到达JavaFX屏幕中的某个点之前会变成红色 我想要它做的是,当它到达点x,y:(600500)时,使蓝色矩形变成红色JAVA FX-时间轴动画(在动画期间查找特定的x、y点),java,javafx,Java,Javafx,在这个程序中,当for循环中的x==600时,我试图使rect变为红色。基本上,for循环的运行速度比屏幕上的动画快。矩形在实际到达JavaFX屏幕中的某个点之前会变成红色 我想要它做的是,当它到达点x,y:(600500)时,使蓝色矩形变成红色 import java.util.logging.Level; import java.util.logging.Logger; import javafx.animation.KeyFrame; import javafx.animation.Key
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author Owner
*/
public class TestPoint extends Application {
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Scene scene = new Scene(root, 1000, 1000);
Rectangle rect = new Rectangle();
Rectangle rectTwo = new Rectangle();
//Obstacle that other square must hit
rectTwo.setWidth(100);
rectTwo.setHeight(100);
rectTwo.setX(500);
rectTwo.setY(500);
rectTwo.setFill(Color.PINK);
//for loop that causes the animation to properly move
for (int x = 800; x >= 0; x--) {
rect.setWidth(100);
rect.setHeight(100);
rect.setX(800);
rect.setY(500);
rect.setFill(Color.BLUE);
Timeline timeline = new Timeline();
timeline.setCycleCount(1);
timeline.setAutoReverse(true);
final KeyValue kv = new KeyValue(rect.xProperty(), x);
final KeyFrame kf = new KeyFrame(Duration.seconds(8), kv);
timeline.getKeyFrames().add(kf);
timeline.play();
//if it hits the point of rectTwo, change to Color.RED
System.out.println(x);
if (x == 600) {
rect.setFill(Color.RED);
break;//end
}
}
root.getChildren().addAll(rect, rectTwo);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
您误解了
时间线的工作原理。您的代码创建了201个并行运行的时间线动画。循环在显示窗口之前完成。JavaFX稍后会自动触发任何更新
通过KeyFrame
s指定初始状态和目标状态就足够了<代码>关键帧允许您指定在特定时间执行的处理程序;这可以用来改变颜色。或者,可以使用onFinished
处理程序为矩形上色
rect.setWidth(100);
rect.setHeight(100);
rect.setY(500);
rect.setFill(Color.BLUE);
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(rect.xProperty(), 800)),
new KeyFrame(Duration.seconds(8),
evt -> rect.setFill(Color.RED),
new KeyValue(rect.xProperty(), 600)));
timeline.play();
您误解了时间线的工作原理。您的代码创建了201个并行运行的时间线动画。循环在显示窗口之前完成。JavaFX稍后会自动触发任何更新
通过KeyFrame
s指定初始状态和目标状态就足够了<代码>关键帧允许您指定在特定时间执行的处理程序;这可以用来改变颜色。或者,可以使用onFinished
处理程序为矩形上色
rect.setWidth(100);
rect.setHeight(100);
rect.setY(500);
rect.setFill(Color.BLUE);
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(rect.xProperty(), 800)),
new KeyFrame(Duration.seconds(8),
evt -> rect.setFill(Color.RED),
new KeyValue(rect.xProperty(), 600)));
timeline.play();
我也可以使用碰撞检测器,比如BooleanBinding,还是这种方法更好?对于碰撞检测,您可以使用一个关键帧
和一个持续时间很短的事件处理程序触发逐步更新,以执行“更新循环”,允许您在更新完成后执行碰撞检查,您可以使用单独的AnimationTimer
或将侦听器注册到矩形的x
属性…如果创建更复杂的动画,侦听器方法最有可能重复检查。我如何继续创建矩形的x属性的侦听器?有我可以看的教程吗?只需查看addListener
方法:和。即使不做教程,实现其中一个接口也不会太难。示例:rect.xProperty().addListener(o->{…})
我也可以使用碰撞检测器,比如BooleanBinding,还是这种方法更好?对于碰撞检测,您可以使用单个关键帧
和一个持续时间很短的事件处理程序触发逐步更新,以执行“更新循环”,允许您在更新完成后执行碰撞检查,您可以使用单独的AnimationTimer
或将侦听器注册到矩形的x
属性…如果创建更复杂的动画,侦听器方法最有可能重复检查。我如何继续创建矩形的x属性的侦听器?有我可以看的教程吗?只需查看addListener
方法:和。即使不做教程,实现其中一个接口也不会太难。示例:rect.xProperty().addListener(o->{…})代码>参见