java 基于图块的游戏 ArrayIndexOutOfBoundsException 错误
java Tile based game ArrayIndexOutOfBoundsException error
我在 World class 中的 getTile 函数中出现 ArrayIndexOutOfBoundsException 错误,我的问题是我无法用背景图像铺满背景,我无法添加其他瓷砖我得到空 window 和这些错误。
package com.game.Tiles;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Tile {
// static stuff
public static Tile[] tiles = new Tile[512];
public static Tile background = new Background(0);
public static Tile target = new Target(1);
// class
public static final int TILE_WIDTH = 50, TILE_HEIGHT = 50;
protected BufferedImage texture;
protected final int id;
public Tile(BufferedImage texture, int id) {
super();
this.texture = texture;
this.id = id;
tiles[id] = this; // class ı tile a ekliyoruz yukardan gelen id ile
}
public void tick(){
}
public void render(Graphics graphic, int positionX, int positionY){
graphic.drawImage(texture, positionX, positionY, TILE_WIDTH, TILE_HEIGHT, null);
}
public int getId() {
return this.id;
}
public boolean isPasseble()
{
return false;
}
}
package com.game.world;
import java.awt.Graphics;
import com.game.Tiles.Tile;
public class World {
private int height, width;
private int[][] tiles;
public World(){
this.loadWorld();
}
public void tick(){
}
public void render(Graphics graphic){
for (int y = 0; y < this.height; y++) {
for(int x = 0; y < this.width; x++){
this.getTile(x, y).render(graphic, x * Tile.TILE_HEIGHT, y * Tile.TILE_WIDTH);
}
}
}
public Tile getTile(int x, int y){
Tile t = Tile.tiles[this.tiles[x][y]];
if(t == null)
return Tile.background;
return t;
}
private void loadWorld(){
this.width = 30;
this.height = 30;
this.tiles = new int[this.width][this.height];
for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) {
this.tiles[x][y] = 0;
}
}
}
}
错误:
Exception in thread "Thread-0"
java.lang.ArrayIndexOutOfBoundsException: 30
at com.game.world.World.getTile(World.java:28)
at com.game.world.World.render(World.java:23)
at com.game.States.GameState.render(GameState.java:29)
at com.game.Launcher.Game.render(Game.java:73)
at com.game.Launcher.Game.run(Game.java:98)
at java.lang.Thread.run(Thread.java:745)
这一行应该
for(int x = 0; y < this.width; x++){
应该是
for(int x = 0; x < this.width; x++){
我在 World class 中的 getTile 函数中出现 ArrayIndexOutOfBoundsException 错误,我的问题是我无法用背景图像铺满背景,我无法添加其他瓷砖我得到空 window 和这些错误。
package com.game.Tiles;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
public class Tile {
// static stuff
public static Tile[] tiles = new Tile[512];
public static Tile background = new Background(0);
public static Tile target = new Target(1);
// class
public static final int TILE_WIDTH = 50, TILE_HEIGHT = 50;
protected BufferedImage texture;
protected final int id;
public Tile(BufferedImage texture, int id) {
super();
this.texture = texture;
this.id = id;
tiles[id] = this; // class ı tile a ekliyoruz yukardan gelen id ile
}
public void tick(){
}
public void render(Graphics graphic, int positionX, int positionY){
graphic.drawImage(texture, positionX, positionY, TILE_WIDTH, TILE_HEIGHT, null);
}
public int getId() {
return this.id;
}
public boolean isPasseble()
{
return false;
}
}
package com.game.world;
import java.awt.Graphics;
import com.game.Tiles.Tile;
public class World {
private int height, width;
private int[][] tiles;
public World(){
this.loadWorld();
}
public void tick(){
}
public void render(Graphics graphic){
for (int y = 0; y < this.height; y++) {
for(int x = 0; y < this.width; x++){
this.getTile(x, y).render(graphic, x * Tile.TILE_HEIGHT, y * Tile.TILE_WIDTH);
}
}
}
public Tile getTile(int x, int y){
Tile t = Tile.tiles[this.tiles[x][y]];
if(t == null)
return Tile.background;
return t;
}
private void loadWorld(){
this.width = 30;
this.height = 30;
this.tiles = new int[this.width][this.height];
for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) {
this.tiles[x][y] = 0;
}
}
}
}
错误:
Exception in thread "Thread-0" java.lang.ArrayIndexOutOfBoundsException: 30 at com.game.world.World.getTile(World.java:28) at com.game.world.World.render(World.java:23) at com.game.States.GameState.render(GameState.java:29) at com.game.Launcher.Game.render(Game.java:73) at com.game.Launcher.Game.run(Game.java:98) at java.lang.Thread.run(Thread.java:745)
这一行应该
for(int x = 0; y < this.width; x++){
应该是
for(int x = 0; x < this.width; x++){