获取某个时间线后的int值
Get the value of a int after a timeline
我有一个应用程序,其目标是掷骰子,我使用了时间线,但我不知道如何在另一个 class 中获取它最后的骰子值。
这是我的代码:
在 class 骰子
private List <Image> listeFaceDice;
private int randomNum;
private ImageView imageView;
public void dropDice(){
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), event -> {
setRandomNum(rand.nextInt(6);
imageView.setImage(listeFaceDice.get(randomNum-1));
}));
timeline.setCycleCount(6);
timeline.play();
timeline.setOnFinished(e -> {
setRandomNum(randomNum);
});
}
在class游戏中
public Button getBtnDropDice() {
if(btnDropDice == null) {
btnDropDice = new Button("Drop dice");
btnDropDice.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
Dice dice = new Dice();
dice.dropDice();
System.out.println(dice.getRandomNum());
}
});
}
return btnDropDice;
}
您实际上已经在掷骰子完成后访问了该值:您只是不对其进行任何操作(除了调用 setRandomNum(...)
,它在您传递值时不执行任何操作已经设置)。
如果您用 System.out.println(...)
替换该处理程序,您将在控制台中看到该值:
timeline.setOnFinished(e -> {
System.out.println(randomNum);
});
如果你想在调用 class 时用它做一些事情,当然首先要注意 dropDice()
方法将立即退出(即在动画完成之前)。您可以做的一件事是向 dropDice()
方法传递一个处理结果的函数:
public void dropDice(IntConsumer valueProcessor){
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), event -> {
setRandomNum(rand.nextInt(6);
imageView.setImage(listeFaceDice.get(randomNum-1));
}));
timeline.setCycleCount(6);
timeline.play();
timeline.setOnFinished(e -> {
valueProcessor.accept(randomNum);
});
}
现在你可以做:
Dice dice = new Dice();
dice.dropDice(diceValue -> {
// do whatever you need to do with diceValue here.
// Just as a demo:
System.out.println("Value rolled was "+diceValue);
});
您传递给dropDice()
的函数(更准确地说,IntConsumer
中的accept(...)
方法)将在FX应用程序线程上调用(因此更新是安全的UI) 一旦掷骰完成。
我有一个应用程序,其目标是掷骰子,我使用了时间线,但我不知道如何在另一个 class 中获取它最后的骰子值。 这是我的代码: 在 class 骰子
private List <Image> listeFaceDice;
private int randomNum;
private ImageView imageView;
public void dropDice(){
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), event -> {
setRandomNum(rand.nextInt(6);
imageView.setImage(listeFaceDice.get(randomNum-1));
}));
timeline.setCycleCount(6);
timeline.play();
timeline.setOnFinished(e -> {
setRandomNum(randomNum);
});
}
在class游戏中
public Button getBtnDropDice() {
if(btnDropDice == null) {
btnDropDice = new Button("Drop dice");
btnDropDice.setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent arg0) {
// TODO Auto-generated method stub
Dice dice = new Dice();
dice.dropDice();
System.out.println(dice.getRandomNum());
}
});
}
return btnDropDice;
}
您实际上已经在掷骰子完成后访问了该值:您只是不对其进行任何操作(除了调用 setRandomNum(...)
,它在您传递值时不执行任何操作已经设置)。
如果您用 System.out.println(...)
替换该处理程序,您将在控制台中看到该值:
timeline.setOnFinished(e -> {
System.out.println(randomNum);
});
如果你想在调用 class 时用它做一些事情,当然首先要注意 dropDice()
方法将立即退出(即在动画完成之前)。您可以做的一件事是向 dropDice()
方法传递一个处理结果的函数:
public void dropDice(IntConsumer valueProcessor){
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(0.5), event -> {
setRandomNum(rand.nextInt(6);
imageView.setImage(listeFaceDice.get(randomNum-1));
}));
timeline.setCycleCount(6);
timeline.play();
timeline.setOnFinished(e -> {
valueProcessor.accept(randomNum);
});
}
现在你可以做:
Dice dice = new Dice();
dice.dropDice(diceValue -> {
// do whatever you need to do with diceValue here.
// Just as a demo:
System.out.println("Value rolled was "+diceValue);
});
您传递给dropDice()
的函数(更准确地说,IntConsumer
中的accept(...)
方法)将在FX应用程序线程上调用(因此更新是安全的UI) 一旦掷骰完成。