简体   繁体   English

我在尝试加载大于4的地图时遇到错误

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

相关问题 尝试 setOnClickListener 时出现错误 - I am getting error when trying to setOnClickListener 为什么我在尝试使用 LWJGL 库加载声音时收到错误“NoClassDefFoundError: sun/misc/Unsafe”? - Why am I getting the error “NoClassDefFoundError: sun/misc/Unsafe” when trying to load sounds using the LWJGL library? 为什么在尝试与我的服务器通信时出现此错误? - Why am i getting this error when trying to communicate with my server? 尝试使用 AWS CodeGuru 时出现此错误 - I am getting this error when trying to user AWS CodeGuru 当我试图解密获得以下错误 - When I am trying to decrypt getting following error 尝试创建 Singleton 时出现 getInstance() 错误 - I am getting a getInstance() error occurs when trying to create a Singleton 为什么在尝试实现 ViewModel 时出现此错误? - Why am i getting this error when trying to Implement ViewModel? 可以告诉我为什么我尝试在地图中添加浮点数时出现以下错误,即使我的浮点数是从Number继承的 - Could any on tell why am I getting following error when I am trying to add float number in the map even though my float is inherited from Number Libgdx 当我尝试加载 Tiled 时不断出现错误 Map - Libgdx Keep getting an error when I try to load a Tiled Map 尝试开始运行jabaco项目时出现错误 - I am getting some error when I am trying to start running jabaco project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM