SequentialTransition的重复次数超过了cylcecount

时间:2018-12-11 06:15:50

标签: java javafx-8 race-condition

我本来会写故障,但是我知道我将要学习一个我不知道的Javafx函数。

当我使用PauseTransitionSequentialTransition的组合来制作一个由四个网格的按钮组成的四连体游戏时,每个按钮都会向下浮动。在一个非常特定的用例下,我有一个非常意外的结果。

这里是运行和复制经编辑后的错误的所有代码,这些错误使它们独立且简单,同时仍保留基本的游戏逻辑。

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 chosen
i is: 2  but cycle count is: 2
The button 0 1 has been chosen
false
The button1 1has been chosen
i is: 0  but cycle count is: 0
false
The button4 2has been chosen
i is: 4  but cycle count is: 4
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
THis should never happen, row is: 0
...
...
The button 0 2 has been chosen

要重现错误/竞赛条件: 1.选择任何按钮,然后按一下。 2.选择立即变色的按钮上方的按钮,然后按转弯。 3.选择一个不在按钮底部行中的按钮,然后观察魔术的发生。

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

如果有人对JavaFX的这一领域有任何经验,我会很感激,因为我很茫然。

0 个答案:

没有答案