我如何创建一个每 0.1 秒运行一次并且可以向其中传递一个数组的可执行文件
How do i create an excecutable that runs every 0.1 seconds and can have an array passed into it
我正在制作游戏。
我创建了一个包含我所有形状的数组。
我需要每 0.1 秒或更小的时间移动一次形状。
同时我的鼠标后面也有一个圆圈。
我需要能够一直跟随我的鼠标移动圆圈,但我还没有找到每秒移动其他圆圈一点点的方法。
当我试图移动其他圆圈时,它们会在 javaFX window 加载之前立即全部移动。
这是我的数组
我创建了一个圆并将其添加到我的形状数组中
ArrayList<Shape> Objects = new ArrayList<Shape>();
Circle I = ClassObjects.getCircle(randomWidth,randomX,randomY);
Objects.add(I);
我试过用
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(Timingobjects::myTask, 0, 1, TimeUnit.SECONDS);
}// program name, function, wait before start, time before each loop
private static void myTask() {
System.out.println("Running");
}
但是我发现这是一个运行nable executor service,所以没有return值可以传递出去,我也无法得到要传递的数组。
我试图查看可调用执行程序服务,但我无法理解如何对其进行编码。
如果有人可以给我一个可执行文件的示例,该可执行文件每 0.1 秒 运行 并且可以将我的形状数组列表传递给它,以便可以对其进行更改,然后 returned 退出。
非常感谢。
TimerTask
是可能的。扩展 TimerTask
如:
class CustomTimerTask extends TimerTask {
List<Shape> shapes;
public CustomTimerTask(List<Shape> shapes, /* observers*/) {
this.shapes = shapes;
}
@Override
public void run() {
// update shapes
// notify( about mouse events or whatever)
}
}
不要尝试 return 任何值。使用观察者模式通知订阅者。
现在
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new CustomTimerTask(shapes/*, */), 0, 0.1 * 1000);
示例见Timeline in the Oracle Animation and Visual Effects in JavaFX使用教程。
否则你可以使用 AnimationTimer.
您不需要将数组传递给时间轴或 AnimationTimer,因为节点将存储在场景图中,您可以直接从时间轴或 AnimationTimer 事件处理代码中操作场景图节点。
例如创建一个每 0.1 秒处理一次的时间线:
final Timeline timeline = new Timeline();
timeline.setCycleCount(Animation.INDEFINITE);
timeline.getKeyFrames().add(
new KeyFrame(Duration.millis(100)),
new KeyValue(node.translateXProperty(), 25))
);
timeline.play();
请注意,JavaFX 中的帧速率默认以每秒 60 帧处理。所以 .1 秒时间轴不会准确地每 .1 秒调用一次。
还相关:
- JavaFX periodic background task
- Timers and javafx
我正在制作游戏。 我创建了一个包含我所有形状的数组。 我需要每 0.1 秒或更小的时间移动一次形状。 同时我的鼠标后面也有一个圆圈。 我需要能够一直跟随我的鼠标移动圆圈,但我还没有找到每秒移动其他圆圈一点点的方法。 当我试图移动其他圆圈时,它们会在 javaFX window 加载之前立即全部移动。
这是我的数组
我创建了一个圆并将其添加到我的形状数组中
ArrayList<Shape> Objects = new ArrayList<Shape>();
Circle I = ClassObjects.getCircle(randomWidth,randomX,randomY);
Objects.add(I);
我试过用
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.scheduleAtFixedRate(Timingobjects::myTask, 0, 1, TimeUnit.SECONDS);
}// program name, function, wait before start, time before each loop
private static void myTask() {
System.out.println("Running");
}
但是我发现这是一个运行nable executor service,所以没有return值可以传递出去,我也无法得到要传递的数组。
我试图查看可调用执行程序服务,但我无法理解如何对其进行编码。
如果有人可以给我一个可执行文件的示例,该可执行文件每 0.1 秒 运行 并且可以将我的形状数组列表传递给它,以便可以对其进行更改,然后 returned 退出。
非常感谢。
TimerTask
是可能的。扩展 TimerTask
如:
class CustomTimerTask extends TimerTask {
List<Shape> shapes;
public CustomTimerTask(List<Shape> shapes, /* observers*/) {
this.shapes = shapes;
}
@Override
public void run() {
// update shapes
// notify( about mouse events or whatever)
}
}
不要尝试 return 任何值。使用观察者模式通知订阅者。
现在
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new CustomTimerTask(shapes/*, */), 0, 0.1 * 1000);
示例见Timeline in the Oracle Animation and Visual Effects in JavaFX使用教程。
否则你可以使用 AnimationTimer.
您不需要将数组传递给时间轴或 AnimationTimer,因为节点将存储在场景图中,您可以直接从时间轴或 AnimationTimer 事件处理代码中操作场景图节点。
例如创建一个每 0.1 秒处理一次的时间线:
final Timeline timeline = new Timeline();
timeline.setCycleCount(Animation.INDEFINITE);
timeline.getKeyFrames().add(
new KeyFrame(Duration.millis(100)),
new KeyValue(node.translateXProperty(), 25))
);
timeline.play();
请注意,JavaFX 中的帧速率默认以每秒 60 帧处理。所以 .1 秒时间轴不会准确地每 .1 秒调用一次。
还相关:
- JavaFX periodic background task
- Timers and javafx