我正在創建一個選擇您自己的冒險「遊戲」作爲組項目的一部分。我的想法是創建一個StoryRoom類型的對象,它可以存儲諸如StoryRoom的描述,其他StoryRooms連接到它的信息等。我還想創建一個冒險類,這將成爲一個StoryRooms數組。這個想法是,主程序將在StoryRooms中循環,顯示文本並在作出決定時返回一個值,該值將成爲下一個StoryRoom的參考,直到玩家達到最終場景。在一個類中構建對象並在另一個類中訪問它們
我遇到的問題是我無法通過AdventureTeller類訪問在AdventureBuilder類中創建的StoryRooms。 AdventureTeller是遊戲的驅動程序類。 Eclipse會拋出一個入口無法作爲變量解析的錯誤。我怎樣才能解決這個問題?
public class StoryRoom {
public String[] description = null;
public int[] exits;
public StoryRoom(String[] builddescriptions, int[] buildexits){
description = new String[builddescriptions.length];
exits = new int[buildexits.length];
for (int i = 0; i< builddescriptions.length; i++){
description[i] = builddescriptions[i];
}
for (int i = 0; i< buildexits.length; i++){
exits[i] = buildexits[i];
}
}
public String GetDescription(int reference){
return description[reference];
}
public int GetExit(int reference){
return exits[reference];
}
}
public class AdventureTeller {
public static void main(String[] args) {
AdventureBuilder.main();
for (int i = 0; i < entryway.description.length; i++)
{
System.out.println(entryway.GetDescription(i));
}
}
}
public class AdventureBuilder {
public static void main() {
// StoryRoom[] book = new StoryRoom[20];
String[] description = null;
description = new String[3];
int[] exits;
exits = new int[3];
description[0] = "This is a strange room with 2 doors, one on the left and one on the right.";
description[1] = "Take the left door? (enter 1)";
description[2] = "Take the right door? (enter 2)";
exits[0] = 1; // current room number
exits[1] = 2;
exits[2] = 3;
StoryRoom entryway = new StoryRoom(description, exits);
}
}
我認爲它與對象的範圍有關,並且只要AdventureBuilder的main運行完畢,它們就會被銷燬。我嘗試將對象或數組對象(註釋代碼)作爲返回值傳遞回去,但是並沒有成功。 – 2013-04-22 03:23:46