Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 简单地图读取程序中的子字符串索引关闭_Java_String - Fatal编程技术网

Java 简单地图读取程序中的子字符串索引关闭

Java 简单地图读取程序中的子字符串索引关闭,java,string,Java,String,好的,我正在尝试创建一个简单的RPG游戏,我的地图读取代码似乎已经关闭。 这是地图阅读线: Integer.parseInt(map.data.substring(Map.MAP_START + ((playerPOS - map.width)/2) - 1, Map.MAP_START + ((((playerPOS - map.width)/2) - 1) + map.dataLen))); 现在地图上唯一的瓷砖是01和00,所以当我看到10时,我知道有些地方不对劲: (playerPOS

好的,我正在尝试创建一个简单的RPG游戏,我的地图读取代码似乎已经关闭。 这是地图阅读线:

Integer.parseInt(map.data.substring(Map.MAP_START + ((playerPOS - map.width)/2) - 1, Map.MAP_START + ((((playerPOS - map.width)/2) - 1) + map.dataLen)));
现在地图上唯一的瓷砖是01和00,所以当我看到10时,我知道有些地方不对劲:

(playerPOS - map.width) = 34 playerPOS = 50 player.x = 2 player.y = 3 blah = 0
(playerPOS - map.width) = 18 playerPOS = 34 player.x = 2 player.y = 2 blah = 10
以下是地图读取代码:

public void init(GameContainer gc) throws SlickException {
        map = new Map();
        player = new Player();

        keys = new boolean[ALL_KEYS];
        for(int i = 0; i < ALL_KEYS; i++){
            keys[i] = false;
        }

        file = new File("testmap.txt");

        try {
            fin = new FileInputStream(file);

            bin = new BufferedInputStream(fin);
            StringBuilder sb = new StringBuilder();
            int ch = 0;

            while ((ch=bin.read())!=-1) {
                sb.append((char)ch);
            }

            map.data = sb.toString().replace(" ", "").replace("\n", "").replace("\r", "");
            System.out.print(map.data);

            map.width = Integer.parseInt(map.data.substring(map.dataOffs, map.dataOffs + map.dataLen));
            map.dataOffs += map.dataLen;

            map.height = Integer.parseInt(map.data.substring(map.dataOffs, map.dataOffs + map.dataLen));
            map.dataOffs += map.dataLen;
        }
        catch (Exception e) {
            e.printStackTrace();
        }

        tiles = new Image("tiles.png");
        hero = new Image("hero.png");
    }

因此,如果需要更多信息,请告诉我。

我建议重新考虑地图的数据结构(可能是2D阵列)。在
init
方法中,逐个令牌读取文本文件令牌(假设每个令牌都是空格分隔的)。这与您现在所做的类似,但是可以避免用空字符串替换空白。看

第一个标记字符串(在您的示例中为“16”)可以在您进行转换时转换为
map.width
。同样,第二个标记变为
map.height
。然后,您可以通过以下方式(伪代码)循环剩余的令牌:

for(int y=0;y
然后,当您想知道房间在位置
(x,y)
处的样子时,只需访问
mapArray[x][y]

16 12
01 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00
00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 01 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
for(int y = 0; y < map.height; ++y) {
  for (int x = 0; x < map.width; ++x) {
    mapArray[x][y] = nextTokenAsInteger;
  }
}