我正在尝试重新设计我用Java编写的基于文本的终端rpg,作为Android应用程序。我在游戏的一小部分上工作,玩家可以从一个房间走到另一个房间。
代码停止并提示用户在终端应用程序中输入,现在它只是一直运行到最后并且不会接受任何输入。
public class MainActivity extends AppCompatActivity {
//rooms
Room r1 = new Room("The Great Hall", "A long wide room draped here and there in curtains.");
Room r2 = new Room("The Side Study", "a small cluttered room.");
Room r3 = new Room("The Gardens", "a misty path between beds, once carefully tended now overgrown and disturbed by the encroaching weeds.");
Room r4 = new Room("A Long Passage", "a narrow hallway lined with brooding portraits. Set into the walls to your left and right are blank grey doors, three to a side. They are locked.");
Room currentRoom;
//room maps
HashMap<String, Room> mapR1 = new HashMap<>();
HashMap<String, Room> mapR2 = new HashMap<>();
HashMap<String, Room> mapR3 = new HashMap<>();
HashMap<String, Room> mapR4 = new HashMap<>();
//user
String userDir;
String endgame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//begin setup
//
//map for great hall
mapR1.put("north", r4);
mapR1.put("east", r2);
mapR1.put("west", r3);
r1.setRoomMap(mapR1);
//map for side study
mapR2.put("west", r1);
r2.setRoomMap(mapR2);
//map for gardens
mapR3.put("east", r1);
r3.setRoomMap(mapR3);
//map for long passage
mapR4.put("south", r1);
r4.setRoomMap(mapR4);
//init
currentRoom = r1;
endgame = "non";
//views
final TextView textView = (TextView) findViewById(R.id.youmum);
final EditText editText = (EditText) findViewById(R.id.editText);
//
//end setup
//the actual game play
//
assert textView != null;
textView.setText("You are in the " + currentRoom.getRoomTitle() + "; " + currentRoom.getRoomDescription());
textView.append("\nThere are exits to the, ");
for (String ky : currentRoom.getRoomMap().keySet()) {
textView.append(ky + " ");
}
textView.append("\nChoose a direction");
assert editText != null;
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
private void sendMessage() {
userDir = editText.getText().toString().toLowerCase();
if (userDir.contains("go") || userDir.contains("walk") || userDir.contains("move")) {
boolean notDir = false;
for (String kee : currentRoom.getRoomMap().keySet()) {
if (userDir.contains(kee)) {
currentRoom = currentRoom.getExitRoom(kee);
notDir = false;
break;
} else {
notDir = true;
}
}
if (notDir) {
textView.setText("You cannot go that way.\n");
}
}
}
});
textView.setText("You are in the " + currentRoom.getRoomTitle() + "; " + currentRoom.getRoomDescription());
textView.append("\nThere are exits to the, ");
for (String ky : currentRoom.getRoomMap().keySet()) {
textView.append(ky + " ");
}
textView.append("\nChoose a direction");
}
}
我对Android很新(这实际上是我的第一次尝试)所以我假设我只是遗漏了一些明显的东西。
答案 0 :(得分:0)
底部的代码(显示有关您搬入的新房间的文本)需要在ActionListener中移动,以便每次事件触发时都会运行,而不是只是一次初始化。您可以在更新currentRoom的现有代码之后将其放在sendMessage()中。
您可能已经这样做了,但为了触发IME_ACTION_SEND事件,定义EditText的XML应包括:
android:imeOptions="actionSend"
您可能还想添加一些代码来清除EditText内容,因为当用户点击&#34;发送&#34;后,它将保留在那里。