JavaFX通过按下setOnKeyPressed更新当前位置

JavaFX通过按下setOnKeyPressed更新当前位置,java,javafx,Java,Javafx,我现在正在学习JavaFX,我决定做一个简单的冒险游戏。在文本区域和文本字段中,用户键入其想要前往的方向,将显示具有可用出口的位置说明。但是,我遇到了一个问题:在选择方向后,我必须再次按enter键以正确更新当前位置和可用出口。我发现,在当前版本中,这是由于我更新负责当前位置描述的“loc”变量的方式造成的。我不知道如何修复它,或者如何改变它的工作方式。有人能帮我吗?提前感谢您的帮助 地点类别: package sample.LocationsSystem; import java.util.

我现在正在学习JavaFX,我决定做一个简单的冒险游戏。在文本区域和文本字段中,用户键入其想要前往的方向,将显示具有可用出口的位置说明。但是,我遇到了一个问题:在选择方向后,我必须再次按enter键以正确更新当前位置和可用出口。我发现,在当前版本中,这是由于我更新负责当前位置描述的“loc”变量的方式造成的。我不知道如何修复它,或者如何改变它的工作方式。有人能帮我吗?提前感谢您的帮助

地点类别:

package sample.LocationsSystem;

import java.util.HashMap;
import java.util.Map;

public class Location {
private final int locationID;
private final String description;
private final Map<String, Integer> exits;

public Location(int locationID, String description) {
    this.locationID = locationID;
    this.description = description;
    this.exits = new HashMap<>();
}

public void addExit(String direction, int location) {
    exits.put(direction, location);
}

public int getLocationID() {
    return locationID;
}

public String getDescription() {
    return description;
}

public Map<String, Integer> getExits() {
    return new HashMap<String, Integer>(exits);
}
}
package sample.locations系统;
导入java.util.HashMap;
导入java.util.Map;
公共类位置{
私有最终int位置ID;
私有最终字符串描述;
私人最终地图出口;
公共位置(int locationID,字符串描述){
this.locationID=locationID;
this.description=描述;
this.exits=new HashMap();
}
公共void addExit(字符串方向,int位置){
出口。放置(方向、位置);
}
public int getLocationID(){
返回位置ID;
}
公共字符串getDescription(){
返回说明;
}
公共映射getExits(){
返回新的HashMap(退出);
}
}
包含描述和出口的位置:

package sample.LocationsSystem;

import java.util.HashMap;
import java.util.Map;

public class Locations {

private static Map<Integer, Location> locationsMap = new HashMap<>();

public Locations() {
    initializeMap();
}

public static Map<Integer, Location> getLocationsMap() {
    return locationsMap;
}

public static void initializeMap(){
    locationsMap.put(0, new Location(0,"You entered the dungeon"));
    locationsMap.put(1, new Location(1,"You are in a narrow corridor"));
    locationsMap.put(2, new Location(2,"You are in round basement. You can see many doors around you"));
    locationsMap.put(3, new Location(3,"You are in a dark corridor. Strange noises are coming from the door just before you"));
    locationsMap.put(4, new Location(4,"Location 4"));
    locationsMap.put(5, new Location(5,"It's nothing here. Turn back"));
    locationsMap.put(6, new Location(6,"It's a broom cupboard."));
    locationsMap.put(7, new Location(7,"It's just a corridor"));
    locationsMap.put(8, new Location(8,"You entered into old throne room"));
    locationsMap.put(9, new Location(9,"It looks like an old armory"));
    locationsMap.put(10, new Location(10,"You can see a large pile of gold coins. That's what you came here for."));


    locationsMap.get(0).addExit("W",1);

    locationsMap.get(1).addExit("N",2);
    locationsMap.get(1).addExit("E",0);

    locationsMap.get(2).addExit("N",6);
    locationsMap.get(2).addExit("S",1);
    locationsMap.get(2).addExit("E",3);
    locationsMap.get(2).addExit("W",5);

    locationsMap.get(5).addExit("E",2);

    locationsMap.get(6).addExit("S",2);

    locationsMap.get(3).addExit("E",4);
    locationsMap.get(3).addExit("W",2);

    locationsMap.get(4).addExit("N",7);
    locationsMap.get(4).addExit("W",3);

    locationsMap.get(7).addExit("S",4);
    locationsMap.get(7).addExit("E",8);

    locationsMap.get(8).addExit("N",10);
    locationsMap.get(8).addExit("S",9);
    locationsMap.get(8).addExit("W",7);

    locationsMap.get(9).addExit("N",8);


    Map<String, String> vocabulary = new HashMap<String, String>();
    vocabulary.put("NORTH", "N");
    vocabulary.put("SOUTH", "S");
    vocabulary.put("WEST", "W");
    vocabulary.put("EAST", "E");
}
}
package sample.locations系统;
导入java.util.HashMap;
导入java.util.Map;
公共课地点{
私有静态映射位置Map=newhashmap();
公共场所(){
初始化映射();
}
公共静态映射getLocationsMap(){
返回位置MAP;
}
公共静态无效初始值映射(){
地点地图放置(0,新地点(0,“你进入地牢”);
地点地图(1,新地点(1,“你在一条狭窄的走廊”);
地点地图(2,新地点(2,“你在圆形地下室,你可以看到你周围有很多门”);
地点地图(3,新地点(3,“你在一条黑暗的走廊里,你面前的门发出奇怪的声音”);
位置示意图(4,新位置(4,位置4));
地点地图(5,新地点(5,“这里没什么,往回走”);
地点地图(6,新地点(6,“这是一个扫帚柜”);
地点地图(7,新地点(7,“这只是一条走廊”);
地点地图(8,新地点(8,“你进入了旧王座房间”);
地点地图(9,新地点(9,“它看起来像一个旧军械库”);
地点地图(10,新地点(10,“你可以看到一大堆金币,这就是你来这里的目的。”);
位置map.get(0).addExit(“W”,1);
位置映射get(1).addExit(“N”,2);
位置映射get(1).addExit(“E”,0);
位置map.get(2).addExit(“N”,6);
位置映射get(2).附录(“S”,1);
位置映射get(2).附录(“E”,3);
位置映射get(2).附录(“W”,5);
位置映射get(5).附录(“E”,2);
位置映射get(6).附录(“S”,2);
位置图get(3).附录(“E”,4);
位置映射get(3).附录(“W”,2);
位置map.get(4).addExit(“N”,7);
位置图get(4).附录(“W”,3);
位置映射get(7).附录(“S”,4);
位置图get(7).附录(“E”,8);
位置map.get(8).addExit(“N”,10);
位置图get(8).附录(“S”,9);
位置图get(8).附录(“W”,7);
地址map.get(9).addExit(“N”,8);
映射词汇表=新的HashMap();
把“北”、“N”字放进去;
词汇.put(“SOUTH”,“S”);
put(“WEST”、“W”);
put(“东”、“东”);
}
}
和我的控制器类:

package sample;

import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import sample.Characters.Player;
import sample.FightSystem.Fight;
import sample.LocationsSystem.Locations;

import java.util.Map;


public class Controller {

@FXML
TextField HPTextField;

@FXML
TextField enemyHP;

@FXML
TextField potionsTextField;

@FXML
TextField textField;

@FXML
TextArea mainTextArea;

public void clearTextField() {
    textField.clear();
}

int loc = 0;
public void initialize() {

    Player player = new Player("Player", 100, 10);
    Fight fight = new Fight();

    HPTextField.setText(Integer.toString(player.getHp()));
    potionsTextField.setText(Integer.toString(player.getPotions().size()));

    mainTextArea.setText("Welcome to adventure game! \n=======PRESS ENTER TO START======");

    Locations locations = new Locations();
    Map<String, Integer> exits = locations.getLocationsMap().get(loc).getExits();

    textField.setOnKeyPressed(keyEvent -> {
        if (keyEvent.getCode().equals(KeyCode.ENTER)) {

            String direction = textField.getText().toUpperCase();

            mainTextArea.appendText(locations.getLocationsMap().get(loc).getDescription()
                    + "\n In which direction would you like to go? \nAvailable directions: ");

            for (String exit : exits.keySet()) {
                mainTextArea.appendText(exit + " ");
            }

            if (exits.containsKey(direction)) {
                loc = exits.get(direction);
            }
            textField.clear();
        }
    });
}
}
包装样品;
导入javafx.fxml.fxml;
导入javafx.scene.control.TextArea;
导入javafx.scene.control.TextField;
导入javafx.scene.input.KeyCode;
导入sample.Characters.Player;
导入sample.FightSystem.Fight;
导入sample.locationsystem.Locations;
导入java.util.Map;
公共类控制器{
@FXML
TextField HPTextField;
@FXML
TextField-enemyHP;
@FXML
TextField药剂TextField;
@FXML
TextField;
@FXML
text区域mainTextArea;
public void clearTextField(){
textField.clear();
}
int loc=0;
公共无效初始化(){
玩家=新玩家(“玩家”,100,10);
战斗=新的战斗();
HPTextField.setText(Integer.toString(player.getHp());
potionsTextField.setText(Integer.toString(player.getPotions().size());
mainTextArea.setText(“欢迎来到冒险游戏!\n==========按ENTER开始=====”;
位置=新位置();
映射出口=locations.getLocationsMap().get(loc).getExits();
textField.setOnKeyPressed(按键事件->{
if(keyEvent.getCode().equals(KeyCode.ENTER)){
字符串方向=textField.getText().toUpperCase();
mainTextArea.appendText(locations.getLocationsMap().get(loc).getDescription()
+“\n您想往哪个方向走?\n可用方向:”;
for(字符串出口:exits.keySet()){
mainTextArea.appendText(退出+“”);
}
if(出口。集装箱箱(方向)){
loc=出口。获取(方向);
}
textField.clear();
}
});
}
}

以下是一个示例,旨在让您了解如何进行此操作,并解决当前存在的位置设置问题

示例所做的是移动到起始位置,然后,每当用户移动到新位置时,更新该位置,并在提供的文本区域中向用户提供适当的反馈

问题的关键区别在于,以下代码将立即更新当前位置参考,并在移动到新位置时提供反馈,而不是在下一次按键时

private static final int STARTING_LOC = 0;
private Locations locations = new Locations();
private int loc;

private TextArea mainTextArea = new TextArea();
private TextField textField = new TextField();

public void initialize() {
    mainTextArea.setText("Welcome to adventure game!");
    moveTo(STARTING_LOC);

    textField.setOnKeyPressed(keyEvent -> {
        if (keyEvent.getCode().equals(KeyCode.ENTER)) {
            String direction = textField.getText().toUpperCase();
            Integer newLoc = getCurrentExits().get(direction);
            if (newLoc != null) {
                moveTo(newLoc);
            }

            textField.setText("");
        }
    });
}

private void moveTo(int newLoc) {
    this.loc = newLoc;
    mainTextArea.appendText(
        locations.getLocationsMap().get(loc).getDescription()
            + "\n In which direction would you like to go? \nAvailable directions: "
    );
    for (String exit : getCurrentExits().keySet()) {
        mainTextArea.appendText(exit + " ");
    }
}

private Map<String, Integer> getCurrentExits() {
    return locations.getLocationsMap().get(loc).getExits();
}
private static final int start_LOC=0;
私有位置=新位置();
私人国际联络处;
私有TextArea mainTextArea=新TextArea();
私有文本字段