我目前正在開發一個簡單的 2D 游戲,就像 Eclipse Java 中的 Minecraft。我目前正在嘗試除錯的功能之一是將生成的 2D 噪聲圖保存到與稱為資源的根檔案夾分開的源檔案夾中,然后使用保存的相同檔案生成將在游戲運行時顯示的地圖。我遇到的問題是我的程式不顯示最近生成的保存的地圖,它將顯示以前的地圖(來自其他測驗運行)。
程式能夠顯示最近生成的地圖的不同時間是:
- 我運行程式,關閉它,在 Eclipse 中點擊重繪 ,然后再次運行程式。
- 找到工程檔案,進入bin檔案夾,洗掉之前Java編譯生成的map。
- 注釋掉顯示地圖的代碼,用生成地圖的代碼運行程式,然后用顯示地圖的代碼再次運行程式。
- 在 Eclipse 中打開包含地圖資料的檔案,對其進行更新,然后運行該程式。
- 多次運行該程式,直到它決定更新。
我可以確認程式成功生成了地圖,并在每次運行時保存它,所以我認為在這個意義上沒有任何錯誤。我的假設是程式可以將地圖資料保存到資源源檔案夾但由于某種原因在運行時不可用?這很奇怪,因為程式看起來與存盤地圖資料以顯示給玩家的位置相同。如果有人能指出我正確的方向,或者至少注意到我做錯了什么,我們將不勝感激。
下面是截圖和代碼:
[檔案結構][1]
呼叫物件的主要構造:
public GamePanel() {
this.setPreferredSize(new Dimension(screen_width,screen_height));
this.setBackground(Color.gray);
this.setDoubleBuffered(true);
key_handler = new KeyEvents();
this.addKeyListener(key_handler);
this.setFocusable(true);
player = new Player(this,key_handler);
//generate map and save map data
map_generator = new TileMap(this);
//load saved map data to be displayed
tile_manager = new TileManager(this);
}
保存地圖資料的map_generator物件的功能:
void saveMap(String map) {
Path resource_path = Paths.get("resources","map_files");
String map_path = resource_path.toFile().getAbsolutePath() "\\" map;
try {
OutputStream map_stream = new FileOutputStream(map_path);
for (int r=0; r<map_height; r ) {
String map_row = "";
for (int c=0; c<map_width; c ) {
map_row = map_row map_array[r][c] " ";
}
byte[] bytes = map_row.getBytes();
map_stream.write(bytes);
if (r!=map_height-1)
map_stream.write(10);
}
map_stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
加載要顯示給玩家的地圖資料的 tile_manager 物件的函式
public void loadMap(String map) {
try {
InputStream map_stream = getClass().getResourceAsStream(map);
BufferedReader map_reader = new BufferedReader
(new InputStreamReader(map_stream));
int col = 0;
int row = 0;
while (col < game_panel.col_size && row < game_panel.row_size) {
String map_line = map_reader.readLine();
while (col < game_panel.col_size) {
String numbers[] = map_line.split(" ");
int num = Integer.parseInt(numbers[col]);
map_file[col][row] = num;
col ;
}
if (col == game_panel.col_size) {
col = 0;
row ;
}
}
map_reader.close();
} catch(IOException e) {
e.printStackTrace();
}
}
uj5u.com熱心網友回復:
我能夠成功解決我的問題,現在我的程式使用它生成的地圖資料并在運行時將其顯示給玩家沒有問題。解決方案是使用 VGA 關于使用已知源(在我的情況下為 Windows %APPDATA% 檔案夾)的提示,而不必在程式檔案中讀取和寫入資料(特別考慮到我想讓它成為要匯出的 jar 檔案以后被別人玩)。
對代碼的更改如下: saveMap 函式的頂部:
String working_directory = System.getenv("AppData") "\\" map;
//Path resource_path = Paths.get("resources","map_files");
//String map_path = resource_path.toFile().getAbsolutePath() "\\" map;
loadMap 函式的頂部:
String working_directory = System.getenv("AppData") "\\" map;
try {
//InputStream map_stream = getClass().getResourceAsStream(map);
InputStream map_stream = new FileInputStream(working_directory);
這兩個函式的第一行System.getenv("AppData")允許 Java 獲取 AppData 檔案夾的路徑,該檔案夾在運行最新版本的 Windows 作業系統的計算機中被標記為環境變數。AppData 檔案夾默認是隱藏的,包含三個檔案夾;本地、LocalLow 和漫游。測驗時,地圖資料保存在漫游中,我相信這是默認設定,但我可能錯了。
在研究這一點時,我確實發現這種方法很好,但在用戶運行舊版本的 Windows 作業系統/計算機或他們手動更改系統中的 %APPDATA% 環境變數的獨特邊緣情況下會適得其反。在這些情況下,建議使用 Windows Native API 呼叫,即使它不是默認檔案夾,它也會為您找到該檔案夾??。有一篇堆疊溢位帖子詳細介紹了如何通過以下鏈接通過 java 實作 Native API 呼叫: Is there a Java library to access the native Windows API?
由于我正在制作游戲并計劃將其匯出,因此我也很想展示如何做到這一點,但是一旦程式充實和完善,這將是稍后實施的事情。謝謝大家的幫助和提示!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/470213.html
上一篇:SWT檢查外殼是否超出螢屏范圍