[英]I am getting an Error when trying to load a map bigger than 4
The Error: 错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4
at stockman.mapping.Map.loadMap(Map.java:67)
at stockman.mapping.Map.<init>(Map.java:23)
at stockman.gamestate.Level1State.init(Level1State.java:21)
at stockman.gamestate.GameState.<init>(GameState.java:18)
at stockman.gamestate.Level1State.<init>(Level1State.java:15)
at stockman.gamestate.MenuState.keyPressed(MenuState.java:83)
at stockman.gamestate.GameStateManager.keyPressed(GameStateManager.java:25)
at stockman.main.GamePanel.keyPressed(GamePanel.java:95)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.AWTEventMulticaster.keyPressed(Unknown Source)
at java.awt.Component.processKeyEvent(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
My Map Code: 我的地图代码:
public class Map {
private String path;
private int height, width;
private Block[][] blocks;
public Map(String loadPath)
{
path = loadPath;
loadMap();
}
public void draw(Graphics g){
for(int i = 0; i < blocks.length; i++){
for(int j = 0; j < blocks[0].length; j++){
blocks[j][i].draw(g);
}
}
}
public Block[][] getBlocks(){
return blocks;
}
public void loadMap(){
InputStream is = this.getClass().getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try{
width = Integer.parseInt(br.readLine());
height = Integer.parseInt(br.readLine());
blocks = new Block[height][width];
for(int y = 0; y < height; y++){
String line = br.readLine();
String[] tokens = line.split("\\s+");
for(int x = 0; x < width; x++){
blocks[x][y]= new Block(x * Block.blockSize ,y * Block.blockSize, Integer.parseInt(tokens[x]));
}
}
}catch(NumberFormatException | IOException e){
e.printStackTrace();
}
}
}
My map1.map code: 我的map1.map代码:
4
4
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
I cant get the map to go any bigger than 4, when I try I get the map any bigger I get that error 我不能让地图超过4,当我尝试我得到更大的地图我得到那个错误
This is my block Class: 这是我的块类:
public class Block extends Rectangle { public class Block extends Rectangle {
private static final long serialVersionUID = 1L;
public static final int blockSize = 64;
private int id;
public Block(int x, int y, int id) {
setBounds(x, y, blockSize, blockSize);
this.id = id;
}
public void tick() {
}
public void draw(Graphics g) {
g.setColor(Color.BLACK);
if(id != 0){
g.fillRect(x - (int)GameState.xOffset, y - (int)GameState.yOffset, width, height);
}
}
//getters and setters
public void setID(int id){
this.id = id;
}
public int getID(){
return id;
}
}
I would greatly appreciate it if someone could help me, I'm quite good at programming but this one has got me completely stumped! 如果有人能帮助我,我会非常感激,我非常擅长编程但是这个让我完全难过!
blocks = new Block[height][width];
for(int y = 0; y < height; y++){
String line = br.readLine();
String[] tokens = line.split("\\s+");
for(int x = 0; x < width; x++){
blocks[x][y]= new Block(x * Block.blockSize ,y * Block.blockSize, Integer.parseInt(tokens[x]));
}
}
See how you're setting up the array with blocks[height][width] and then assigning them blocks[x][y] (where x is predictably looping until width and y height) which is the other way around. 看看你如何用块[height] [width]设置数组,然后分配块[x] [y](其中x可预测地循环直到宽度和y高度),这是另一种方式。 That's why you're blowing the array index.
这就是为什么你要吹阵列索引。
Your draw function also looks like it is suspect, for the same reasons. 出于同样的原因,你的绘图功能看起来也是可疑的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.