簡體   English   中英

RPG中長度為16的瓷磚數組的索引16超出范圍

[英]Index 16 out of bounds for length 16 Array of Tiles in RPG

public void draw(Graphics2D g2) {
        
        int worldCol = 0;
        int worldRow = 0;
        
        while(worldCol < map.maxWorldCol && worldRow < map.maxWorldRow) {
            
            int tileNum = mapTileNum[worldCol][worldRow];
            
            int worldX = worldCol * map.tileSize;
            int worldY = worldRow * map.tileSize;
            int screenX = worldX - map.player.worldX + map.player.screenX;
            int screenY = worldY - map.player.worldY + map.player.screenY;
            
            g2.drawImage(tile[tileNum].image, screenX, screenY, map.tileSize, map.tileSize, null);
            worldCol++;
            
            if(worldCol == map.maxWorldCol) {
                worldCol = 0;
                worldRow++;
            }
            
        }

我無法為我的 RPG 繪制瓷磚,我無法弄清楚那個問題是什么。 它以前畫過,但后來我創建了一個相機和一張比我之前的 16 x 12 地圖更大的地圖。 現在是 50x 50,屏幕尺寸仍然是 16 x 12。我的圖塊位於從文本文檔中獲取數據的數組中。

平鋪圖像是鏈接到數字的圖像文件。

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: Index 16 out of bounds for length 16
    at tile.TileManager.draw(TileManager.java:117)
    at vardasAdventure.Map.paintComponent(Map.java:161)

您正在遍歷一個數組,但超出了數組大小。 數組索引(您放在 [] 括號中以設置/檢索數據的數字)從 0 開始並轉到數組長度 -1。 所以一些示例大小將是: 大小:16,索引 0 - 15 - 大小 24,索引 0 - 23 因為 Java 從 0 而不是 1 開始計數(就像 hoomans 那樣)。

相反,您應該將 if 語句移動到 while 循環的頂部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM