I'm new to JavaFX and I am trying to use the translate transition class to move a chess piece to the tile I have clicked. I don't want to use the drag-and-drop method. I want the piece to move with animation but the code I've written doesn't move the piece at all. I would appreciate it if someone could point me in the right direction. I've researched other questions on SO but nothing has helped so far.
'''
Tile[][] tiles = gameHandler.getBoardTiles(); displayBoard(); piecesGrid.setVgap(2); piecesGrid.setHgap(2); for(int row = 0; row < tiles.length; row++) { for(int column = 0; column < tiles[0].length; column++) { if(!tiles[row][column].isOccupied()) { gamestate[row][column] = new ImageView(BLANK); } else { /* Creates a MouseEventListener that allows player interaction with Piece */ int vertical = row; int horizontal = column; Piece piece = tiles[row][column].getPiece(); gamestate[row][column] = new ImageView(piece.getImage()); // TODO: once ai is working, make listener for only player pieces if(piece.getCorp().isCommandAvailable() && piece.isPlayerPiece()){ gamestate[row][column].setOnMouseDragged(mouseEvent -> { clicked(mouseEvent, gamestate[vertical][horizontal]); }); gamestate[row][column].setOnMouseReleased(mouseEvent -> { try { moved(piece, gamestate[vertical][horizontal], vertical, horizontal); } catch (IOException e) { e.printStackTrace(); } }); } } gamestate[row][column].setFitWidth(tileSize.getWidth()); gamestate[row][column].setFitHeight(tileSize.getHeight()); piecesGrid.add(gamestate[row][column], column, row); } } gameboardPane.getChildren().add(piecesGrid); } /** -------- Following classes are for Mouse Event Listeners ----------- **/ private void clicked(MouseEvent event, ImageView image) { final TranslateTransition transition = new TranslateTransition(Duration.millis(100)); transition.setNode(image); transition.setFromX(event.getX()); transition.setFromY(event.getY()); transition.setToY(event.getX() - (float)tileSize.width/2); transition.setToX(event.getY() - (float)tileSize.height/2); transition.play(draw(image)); } private void draw(ImageView image) { image.setX(image.getX()); image.setY(image.getY()); } private void moved(Piece piece, ImageView image, int vertical, int horizontal) throws IOException { ArrayList<MoveHandler> moves = piece.determineMoves(gameHandler.getBoard()); int moveX = Math.round((float)image.getX() / tileSize.width); int moveY = Math.round((float)image.getY() / tileSize.height); /* Determine if the coordinate the space is trying to move to is a valid move */ int destinationCoordinates = (8 * (vertical+moveY)) + (horizontal+moveX); for(MoveHandler move : moves){ if(move.getDestination() == destinationCoordinates) { gameLog.appendText(piece.getColor().toUpperCase() + " " + piece.getName() + ": " + posToString(piece.getCoordinates()) + " -> " + posToString(move.getDestination()) + "\r\n"); gameHandler.setBoard(move.executeMove()); piece.getCorp().switchCorpCommandAvailablity(); switch (piece.getCorp().getCorpName()) { case "king" -> { kingCCStatus.setText("Unavailable"); kingCCStatus.setTextFill(unavailableColor); } case "kingsBishop" -> { rBishopCCStatus.setText("Unavailable"); rBishopCCStatus.setTextFill(unavailableColor); } case "queensBishop" -> { lBishopCCStatus.setText("Unavailable"); lBishopCCStatus.setTextFill(unavailableColor); } } } } displayPieces(); }
'''
https://stackoverflow.com/questions/66946968/implementing-transition-transition-for-chess-piece April 05, 2021 at 08:17AM
没有评论:
发表评论