[英]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.