gpt4 book ai didi

java - SequentialTransition 重复次数超过循环次数

转载 作者:行者123 更新时间:2023-12-02 10:28:58 26 4
gpt4 key购买 nike

我可能会写出故障,但我知道我即将被带到学校学习 Javafx 的一个我不知道存在的功能。

当我使用 PauseTransitionSequentialTransition 的组合来制作由按钮网格组成的连接四个游戏时,按钮一个按钮地向下 float 。在一个非常具体的用例下,我得到了一个非常意想不到的结果。

这里是运行和复制错误所需的所有代码,已编辑为独立且简约,同时仍保留基本的游戏逻辑。

import javafx.animation.PauseTransition;
import javafx.animation.SequentialTransition;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ConnectFourApplication extends Application {
public static final int NUM_COLUMNS = 8;
public static final int NUM_ROWS = 8;
private Button[][] buttons;
private int row =0;
private int column = 0;
private PauseTransition buttonGravityPause;
private Scene scene;
private BorderPane border;
private ConnectEnum turnColor=ConnectEnum.RED;

public enum ConnectEnum{
RED ("Red"), BLACK ("Black");
private String turn;
ConnectEnum( String value) {
this.turn = value;
}
}

@Override
public void start(Stage primaryStage) {
buttons = new Button[NUM_ROWS][NUM_COLUMNS];
GridPane gridpane = new GridPane();
Button turn = new Button("Take turn");
for(int i=0; i<NUM_ROWS; i++){
for(int j = 0; j<NUM_COLUMNS; j++){
buttons[i][j] = new Button("Empty");
buttons[i][j].setMinHeight(20);
buttons[i][j].setMaxWidth(Double.MAX_VALUE);
buttons[i][j].setOnAction(new ButtonHandler<>(i,j));
gridpane.add(buttons[i][j], j, (NUM_ROWS-1)-i);
}
}

buttonGravityPause = new PauseTransition(new Duration(700));
buttonGravityPause.setOnFinished(event-> {
if (this.row>0) {
buttons[this.row - 1][this.column].setStyle("-fx-background-color:blue");
buttons[this.row][this.column].setStyle("");
this.row = this.row - 1;
}else System.out.println("THis should never happen, row is: "+this.row);
});

SequentialTransition s = new SequentialTransition(buttonGravityPause);
s.setOnFinished(floatDown-> {
System.out.println("The button " +row+ " " +column+" has been chosen");
buttons[this.row][this.column].setText(""+getTurn());
buttons[this.row][this.column].setStyle("-fx-background-color:"+getTurn());
changeTurn();
});

turn.setOnAction(e->{
System.out.println("The button " +row+ " " +column+" has been chosen");
int i =0;
while(this.row-i>0 && buttons[this.row-(i+1)][this.column].getText().equals("Empty"))i++;
s.setCycleCount(i);
System.out.println("i is: "+i+" but cycle count is: "+s.getCycleCount());
if(i>0)s.play(); else{
System.out.println("The button " +row+ " " +column+" has been chosen");
buttons[this.row][this.column].setText(""+getTurn());
buttons[this.row][this.column].setStyle("-fx-background-color:"+getTurn());
changeTurn();
}
});

border = new BorderPane();
border.setCenter(gridpane);
border.setBottom(turn);
scene = new Scene(border, 510, 380);

primaryStage.setTitle("Connect Four");
primaryStage.setScene(scene);
primaryStage.show();
}
private ConnectEnum getTurn(){return this.turnColor;}
private void changeTurn(){this.turnColor = this.turnColor==ConnectEnum.RED? ConnectEnum.BLACK:ConnectEnum.RED;}
private void setRowColumn(int row, int column){
this.row=row;
this.column=column;
}
private int getRow(){
return this.row;
}
private int getColumn(){
return this.column;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

class ButtonHandler<T extends Event> implements EventHandler<T> {
private int row;
private int col;

ButtonHandler(int row, int column){
this.row=row;
this.col=column;
}

@Override
public void handle(T event){
if (buttons[getRow()][getColumn()].getText().equals("Empty")) buttons[getRow()][getColumn()].setStyle("");
if (buttons[this.row][this.col].getText().equals("Empty")) {
setRowColumn(this.row, this.col);
buttons[row][col].setStyle("-fx-background-color:blue");
}else System.out.println("This space is occupied");
}
}
}

除了我的情况外,它在所有情况下都能正确运行 1. 选择一个紧邻已放置按钮上方的按钮,然后 2. 下一个选择,如果将其放置在几个空白点上方,将导致以下控制台输出(包括前几个表现良好和预期的语句):

The button2 1has been choseni is: 2  but cycle count is: 2The button 0 1 has been chosenfalseThe button1 1has been choseni is: 0  but cycle count is: 0falseThe button4 2has been choseni is: 4  but cycle count is: 4THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0THis should never happen, row is: 0......The button 0 2 has been chosen

重现错误/竞争条件:1. 选择任意按钮,按轮流。2. 选择紧邻当前彩色按钮顶部的按钮,然后按“轮流”。3. 选择一个不在底行按钮中的按钮,然后观看奇迹发生。

以前这会导致数组越界,但我添加了一个 if 语句,这样我就可以避免错误并尝试阐明正在发生的情况。

如果有人对 JavaFX 这方面有任何经验,我将非常感激,因为我不知所措。

最佳答案

使 SequentialTransition 成为一个字段,并在单击 turn 时构造一个新字段(而不是重用同一实例),解决了问题:

    turn.setOnAction(e->{
int i = 0;
while(row-i>0 && buttons[row-(i+1)][column].getText().equals("Empty")) {
i++;
}

if(i>0) {
s = new SequentialTransition(buttonGravityPause);
s.setAutoReverse(false);
s.setOnFinished(floatDown-> {
buttons[row][column].setText(""+getTurn());
buttons[row][column].setStyle("-fx-background-color:"+getTurn());
changeTurn();
});
s.setCycleCount(i);
s.play();
} else{
buttons[row][column].setText(""+getTurn());
buttons[row][column].setStyle("-fx-background-color:"+getTurn());
changeTurn();
}
});

我不确定为什么重用 SequentialTransition 实例会导致问题。
希望有了解的 friend 可以补充一下。
可以从here复制工作演示代码

关于java - SequentialTransition 重复次数超过循环次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53718417/

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