java - 在 KeyEvent 方法 Java FXML 中读取其他参数
问题描述
我目前正在制作一个 2d 网格游戏,其中角色在 WASD 上移动(以及屏幕按钮上,但这是以后的功能)。我的 keyevent 方法不允许将其他参数读入其中,因此代码无法运行。不知道如何对其进行编码以允许其他参数。这是监听器的代码
理想情况下,我希望能够运行这样的东西:
public void keyPressed(KeyEvent e, Player player, Cell[][] goldPosition etc.) {
if (e.getCode() == KeyCode.W) {
player.moveUp(player);
detectMovement(goldPosition, trapPosition, player, pane_Board, applePosition);
编辑:我被告知要添加可执行代码,所以我确定了大部分内容。但实际上,我只需要知道如何执行允许其中包含额外参数的 keyevent 代码。谢谢
package minergame.gui;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
import minergame.engine.*;
import java.util.Random;
import java.util.Scanner;
public class Controller {
private GameEngine engine = new GameEngine(10);
private int score = 0;
private int d = 5;
@FXML
private Label lbl_Score;
@FXML
private Label lbl_Stamina;
@FXML
private GridPane pane_Board;
@FXML
private BorderPane border_Pane;
@FXML
public void initialize() {
//instantiate player class
Player player = new Player();
//Scoreboard for score and stamina
lbl_Score.setText("Score: " + Integer.toString(score));
lbl_Stamina.setText("Stamina: " + Integer.toString(player.getStamina()));
/**
* TO DO
* HBox scoreboard is irrelevant now. Labels are used, need to be replaced throughout code when updating
*score and stamina etc.
*/
//gold coord list
Cell[][] goldPosition = new Cell[10][10];
//trap coord list
Cell[][] trapPosition = new Cell[10][10];
//apple coord list
Cell[][] applePosition = new Cell[10][10];
//prompt user to select difficulty
//selectDifficulty();
//initialise the game map
createMap(pane_Board, player, goldPosition, trapPosition, applePosition);
//FOR DEBUGGING print gold location
for(int i = 0; i < goldPosition.length; i++) {
for (int j = 0; j < goldPosition.length; j++) {
if (goldPosition[i][j]!=null)
System.out.println("row:"+ i + " col: " + j);
}
}
}
@FXML
public void keyPressed(KeyEvent e) {
//KEYCODE W
if (e.getCode() == KeyCode.W) {
player.moveUp(player);
detectMovement(goldPosition, trapPosition, player, pane_Board, applePosition);
updateGUI(pane_Board, goldPosition, player, trapPosition, applePosition);
} //KEYCODE D
else if (e.getCode() == KeyCode.D) {
player.moveRight(player);
detectMovement(goldPosition, trapPosition, player, pane_Board, applePosition);
updateGUI(pane_Board, goldPosition, player, trapPosition, applePosition);
} //KEYCODE S
else if (e.getCode() == KeyCode.S) {
player.moveDown(player);
detectMovement(goldPosition, trapPosition, player, pane_Board, applePosition);
updateGUI(pane_Board, goldPosition, player, trapPosition, applePosition);
} //KEYCODE A
else if (e.getCode() == KeyCode.A) {
player.moveLeft(player);
detectMovement(goldPosition, trapPosition, player, pane_Board, applePosition);
updateGUI(pane_Board, goldPosition, player, trapPosition, applePosition);
}
}
//this needs to be changed or deleted
public void updateScore(HBox scoreBoard, Player player) {
scoreBoard.getChildren().clear();
scoreBoard.getChildren().add(new Text("Score: " + Integer.toString(score)));
scoreBoard.getChildren().add(new Text("Stamina: " + Integer.toString(player.getStamina())));
}
@FXML
public void detectMovement(Cell[][] goldPosition, Cell[][] trapPosition, Player player,
GridPane pane, Cell[][] applePosition) {
//detect gold
if (goldPosition[player.getPlayerRow()][player.getPlayerCol()] instanceof Gold) {
pane.getChildren().clear();
goldPosition[player.getPlayerRow()][player.getPlayerCol()] = null;
pane.add(engine.getMap()[player.getPlayerRow()][player.getPlayerCol()] = player, player.getPlayerCol(),
player.getPlayerRow());
score++;
player.setStamina(player.getStamina()+1);
//detect trap
} else if (trapPosition[player.getPlayerRow()][player.getPlayerCol()] instanceof Trap) {
pane.getChildren().clear();
pane.add(engine.getMap()[player.getPlayerRow()][player.getPlayerCol()] = player, player.getPlayerCol(),
player.getPlayerRow());
player.setStamina(player.getStamina() - 4);
//detect apple
} else if (applePosition[player.getPlayerRow()][player.getPlayerCol()] instanceof Apple) {
pane.getChildren().clear();
applePosition[player.getPlayerRow()][player.getPlayerCol()] = null;
pane.add(engine.getMap()[player.getPlayerRow()][player.getPlayerCol()] = player, player.getPlayerCol(),
player.getPlayerRow());
player.setStamina(player.getStamina()+3);
} else {
pane.getChildren().clear();
pane.add(engine.getMap()[player.getPlayerRow()][player.getPlayerCol()] = player, player.getPlayerCol(),
player.getPlayerRow());
}
}
@FXML
public void updateGUI(GridPane pane, Cell[][] goldPosition, Player player,
Cell[][] trapPosition, Cell[][] applePosition) {
//repopulate grid
for (int i = 0; i < engine.getSize(); i++) {
for (int j = 0; j < engine.getSize(); j++) {
pane.add(engine.getMap()[i][j] = new Cell(), j, i);
}
}
addGold(goldPosition, pane_Board);
addTrap(trapPosition, pane_Board);
addApple(applePosition, pane_Board);
//updateScore(scoreBoard, player);
}
public void selectDifficulty() {
Scanner input = new Scanner(System.in);
System.out.println("Let's play! Enter difficulty to start (0 - 10)");
d = input.nextInt();
}
@FXML
public void createMap(GridPane pane, Player player, Cell[][] goldPosition, Cell[][] trapPosition,
Cell[][] applePosition) {
//create map
for (int i = 0; i < engine.getSize(); i++) {
for (int j = 0; j < engine.getSize(); j++) {
pane.add(engine.getMap()[i][j] = new Cell(), j, i);
}
}
//add player to random position on map
Random random = new Random();
player.setPlayerRow(random.nextInt(engine.getSize()));
player.setPlayerCol(random.nextInt(engine.getSize()));
pane.add(engine.getMap()[player.getPlayerRow()][player.getPlayerCol()] = player, player.getPlayerCol(),
player.getPlayerRow());
//add gold to random positions on map
int goldCount = 0;
while(goldCount < 10) {
int randomRow2 = random.nextInt(engine.getSize());
int randomCol2 = random.nextInt(engine.getSize());
if (randomRow2 != player.getPlayerRow() && randomCol2 != player.getPlayerCol() &&
!(goldPosition[randomRow2][randomCol2] instanceof Gold)) {
goldCount++;
goldPosition[randomRow2][randomCol2] = new Gold();
}
}
addGold(goldPosition, pane_Board);
//add traps to random positions on map
int trapCount = 0;
while(trapCount < d) {
int randomRow3 = random.nextInt(engine.getSize());
int randomCol3 = random.nextInt(engine.getSize());
if (randomRow3 != player.getPlayerRow() && randomCol3 != player.getPlayerCol() &&
!(goldPosition[randomRow3][randomCol3] instanceof Gold) &&
!(goldPosition[randomRow3][randomCol3] instanceof Trap)) {
trapPosition[randomRow3][randomCol3] = new Trap();
trapCount++;
}
}
addTrap(trapPosition, pane_Board);
//add apples to random positions on map
int appleCount = 0;
while(appleCount < (15 - d)) {
int randomRow4 = random.nextInt(engine.getSize());
int randomCol4 = random.nextInt(engine.getSize());
if (randomRow4 != player.getPlayerRow() && randomCol4 != player.getPlayerCol() &&
!(goldPosition[randomRow4][randomCol4] instanceof Apple) &&
!(goldPosition[randomRow4][randomCol4] instanceof Gold) &&
!(trapPosition[randomRow4][randomCol4] instanceof Trap)) {
applePosition[randomRow4][randomCol4] = new Apple();
appleCount++;
}
}
addApple(applePosition, pane_Board);
}
public void addGold(Cell[][] list, GridPane pane) {
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list.length; j++) {
if (list[i][j] != null)
pane.add(list[i][j] = new Gold(), j, i);
}
}
}
public void addTrap(Cell[][] list, GridPane pane) {
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list.length; j++) {
if (list[i][j] != null)
pane.add(list[i][j] = new Trap(), j, i);
}
}
}
public void addApple(Cell[][] list, GridPane pane) {
for (int i = 0; i < list.length; i++) {
for (int j = 0; j < list.length; j++) {
if (list[i][j] != null)
pane.add(list[i][j] = new Apple(), j, i);
}
}
}
}
FXML 代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<BorderPane id="border_Pane" onKeyPressed="#keyPressed" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="minergame.gui.Controller">
<top>
<HBox id="scoreBoard" prefHeight="49.0" prefWidth="600.0" BorderPane.alignment="CENTER">
<children>
<Label fx:id="lbl_Score" prefHeight="17.0" prefWidth="142.0" text="Score">
<font>
<Font size="20.0" />
</font>
</Label>
<Label fx:id="lbl_Stamina" prefHeight="17.0" prefWidth="142.0" text="Stamina">
<font>
<Font size="20.0" />
</font>
</Label>
</children></HBox>
</top>
<center>
<GridPane fx:id="pane_Board" BorderPane.alignment="CENTER">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="10" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" percentWidth="10" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" percentHeight="10" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" percentHeight="10" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" percentHeight="10" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
</GridPane>
</center>
<right>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>
<Button mnemonicParsing="false" text="placeholder" />
</children>
</VBox>
</right>
</BorderPane>
细胞(金、苹果等)
package minergame.engine;
import javafx.scene.layout.Pane;
public class Cell extends Pane {
public Cell() {
setStyle("-fx-border-color: black");
this.setPrefSize(100,100);
}
}
任何提示将非常感谢。谢谢!
解决方案
推荐阅读
- angular - 角度 9 中的 echart 代码错误,但可以在角度 7 中工作
- javascript - 将 JS 函数附加到 pug 以创建动态 url
- prediction - 无人驾驶 AI H2O 中的时间序列
- html - 浮动元素对齐不一致
- javascript - 如何将ngrx效果调用组合到单个流中?
- sql - 为什么我的 Access Max() 子查询不起作用?
- rabbitmq - 如何在 python 中使用 RabbitMQ 进行简单的单元测试?
- mongodb - 如果一个结果匹配,$limit 会停止获取吗?
- javascript - 如何在执行其他代码之前等待循环?
- multithreading - Golang 中的 channel 和 mutex 有什么区别?