private void stopGame() { timeline.pause(); done = true; sp.setOnMouseClicked( e -> { timeline.play(); done = false; mediaPlayer.stop(); mediaPlayer.play(); sp.setOnMouseClicked(null); }); }
public void pause() { myGameLoop.pause(); }
public void init() { setOffsets(); Stage primaryStage = (Stage) getScene().getWindow(); mouseEventHandler = new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent event) { CellView source = (CellView) event.getSource(); if (event.getEventType().equals(MouseEvent.MOUSE_PRESSED)) { if (event.isControlDown()) { int x = (int) Math.floor( (event.getScreenX() - primaryStage.getX() - dxProperty.get()) / cellSizeProperty.get()); int y = (int) Math.floor( (event.getScreenY() - primaryStage.getY() - dyProperty.get()) / cellSizeProperty.get()); for (int i = 0; i < pattern.length; i++) { for (int j = 0; j < pattern[0].length; j++) { try { if (cellViews[x + j][y + i].getState() != pattern[i][j]) cellViews[x + j][y + i].incrementState(); } catch (ArrayIndexOutOfBoundsException e) { } } } } else { source.incrementState(); currentState = source.getState(); } } else if (event.getEventType().equals(MouseEvent.MOUSE_DRAGGED)) { int x = (int) Math.floor( (event.getScreenX() - primaryStage.getX() - dxProperty.get()) / cellSizeProperty.get()); int y = (int) Math.floor( (event.getScreenY() - primaryStage.getY() - dyProperty.get()) / cellSizeProperty.get()); if (cellViews[x][y].getState() != currentState) cellViews[x][y].incrementState(); } } }; getChildren().clear(); for (int i = 0; i < grid.getWidth(); i++) { for (int j = 0; j < grid.getHeight(); j++) { CellView cellView = null; if ((i <= 1 || j <= 1) || (grid.getCell(i, j).getState() != 0)) { cellView = new CellView(grid.getCell(i, j)); cellView.widthProperty().bind(cellSizeProperty); cellView.heightProperty().bind(cellSizeProperty); cellView.addEventHandler(MouseEvent.ANY, mouseEventHandler); add(cellView, i, j); } cellViews[i + xOffset][j + yOffset] = cellView; totalCells--; } } timeline = new Timeline(new KeyFrame(new Duration(100), ((event) -> updateGrid()))); timeline.setCycleCount(Timeline.INDEFINITE); getScene() .setOnKeyPressed( (keyEvent) -> { if (keyEvent.getCode().equals(KeyCode.SPACE)) { if (playing) timeline.pause(); else timeline.play(); playing = !playing; } else if (keyEvent.getCode().equals(KeyCode.MINUS)) { if (keyEvent.isControlDown() && cellSizeProperty.get() > 2) zoomOut(); else if (playing && timeline.getRate() > 0.03125) timeline.setRate(timeline.getRate() / 2); } else if (keyEvent.getCode().equals(KeyCode.EQUALS)) { if (keyEvent.isControlDown() && cellSizeProperty.get() < 64) zoomIn(); else if (playing && timeline.getRate() < 16) timeline.setRate(timeline.getRate() * 2); } }); timeline.setRate(timelineRate); if (playing) timeline.play(); ((Stage) getScene().getWindow()).sizeToScene(); }
public void pause() { timeline.pause(); }