我使用MapEditor选择了我用我特定的TileMaps创建的地图。我的问题是:
当导出已创建的地图VIA JSON时,通常看起来大多数地图都显示为其地块ID,如下面的代码段所示。
{
"data":[1, 2, 1, 2, 3, 1, 3, 1, 2, 2, 3, 3, 4, 4, 4, 1], <------ TIle Id's
"height":4,
"name":"ground",
"opacity":1,
"properties":
{
"tileLayerProp":"1"
},
"type":"tilelayer",
"visible":true,
"width":4,
"x":0,
"y":0
}
然而,当我在相同的字段中检查我自己导出的JSON地图时,我看到了,
{
"compression":"zlib",
"data":"eJzt1UFqwzAQQFFvus1Juizdldz\/ViULgQmWItmyJMtv4JGQBirNT9NlmXO+L2qEaXHP30K9u\/RWuq9SYR4rqenZ5+xdlOzrzHlsyJ2Wbe4yWz1KmoS5W4\/UmcLPfiKPsYm10CM9qTPlnDW231SPkiaP5T49UufKbbG1308tcpqs33eHHkfPFttvbouSHmc3GWGOnK9k53uabL1vth7PwjPG1GyRa7bvq9Biz\/TY\/8w9jrR4Te8Wo\/fY871TY2ZtcWQ\/Rz\/rtUaPcVq8z\/peV2wRdvp6\/Fs9XwqejzJndWjdY\/37QpOrTOwuM\/So3ST2v77WtGjQu0etJqFF7F5HptXfQ48WqZ3tbZJqUaNJyw6j9PjU5Cvyek6LVJP312LvuWOPvedocc9ZW\/T4rF2NHuNo3UKPsVrooccV9GqhR\/\/96zFui1F6tDhj7z2P2KPXeXvveOYepXfovd879gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA4v4BxPaYbQ==",
"encoding":"base64",
"height":100,
"name":"solids",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":100,
"x":0,
"y":0
},
这两个都是真实的样本,第一个从他们的文档中提取,第二个来自我的原始导出。你可以看到他们的“数据”字段与我的不同之处在于他们是JSONArray,因此我相信这个函数,
public static void load(String path) throws Exception
{
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(path));
JSONObject jObj = (JSONObject) obj;
JSONArray layers = (JSONArray) jObj.get("layers");
int amount = layers.size();
for (int i = 0; i < amount; i++)
{
JSONObject layer = (JSONObject) layers.get(i);
String type = (String) layer.get("name");
if (type.equals("solids"))
{
WIDTH = (int) ((long)layer.get("width"));
HEIGHT = (int) ((long)layer.get("height"));
solids = parse((JSONArray) layer.get("data"));
}
给了我一些问题。是因为在我的地图的“数据”字段中,我相信这是一个JSONString和一个编码的。
在重新研究之后,似乎我需要解码'数据',并最终以某种方式转变为您在第一个代码块中看到的'数据'字段。
关于我如何做到这一点的任何提示或者仅仅禁止共同编码?
编辑:请求时的我的游戏功能
private static Image[][] parse(JSONArray array)
{
Image[][] layer = new Image[WIDTH][HEIGHT];
int index;
for (int x = 0; x < WIDTH; ++x)
{
for (int y = 0; y < HEIGHT; ++y)
{
index = (int)((long)array.get((y * WIDTH) + x)); //must cast because JSON returns long
layer[x][y] = getSpriteImage(index);
}
}
return layer;
}
编辑2:我的编辑加载功能
public static void load(String path) throws Exception
{
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(path));
JSONObject jObj = (JSONObject) obj;
JSONArray layers = (JSONArray) jObj.get("layers");
int amount = layers.size();
for (int i = 0; i < amount; i++)
{
JSONObject layer = (JSONObject) layers.get(i);
String type = (String) layer.get("name");
if (type.equals("solids"))
{
Decoder decoder = Base64.getDecoder();
byte[] decodedBytes = decoder.decode((byte[]) layer.get("data"));
Inflater decompresser = new Inflater();
solids = parse((JSONArray)decompresser.inflate(decodedBytes));
编辑3:我的getSpriteImage函数
private static Image getSpriteImage(int index)
{
if (index == 0 )
{
return null;
}
index -= 1;
SpriteSheet sheet = Resources.getSprite("tileset");
int verti = sheet.getVerticalCount();
int horiz = sheet.getHorizontalCount();
int y = (index / verti);
int x = (index % horiz);
return sheet.getSubImage(x, y);
}
编辑5:地图的外观
[44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, 44, etcccccccccccccccccccccccccccccccccccccccccccccccc]
编辑6:World&amp; RENDER方法游戏状态
world- Tile.SIZE = 5的最终值;
public static void render(float xRend, float yRend)
{
int offset = 2;
int xStart = (int)(xRend / Tile.SIZE) - offset;
int yStart = (int)(yRend / Tile.SIZE) - offset;
int xEnd = (Window.WIDTH / Tile.SIZE) + xStart + (offset * 2);
int yEnd = (Window.HEIGHT / Tile.SIZE) + yStart + (offset * 2);
for (int x = xStart; x < xEnd; ++x)
{
for (int y = yStart; y < yEnd; ++y)
{
if (solidTileBound(x, y))
{
solids[x][y].draw(x * Tile.SIZE, y * Tile.SIZE, Tile.SIZE, Tile.SIZE);
}
}
}
}
和GameState:
@Override
public void render(GameContainer gc, StateBasedGame stateGame, Graphics gfx) throws SlickException
{
gfx.drawString(("Game State"), 50, 50);
//gfx.translate(-1500, -1500);
World.render(500, 500);
//gfx.resetTransform();
}
编辑7:
[LOG] Loading C:\Users\William.William-PC\Desktop\testMap.json
Sun Dec 06 18:51:33 EST 2015 ERROR:Error loading World
org.newdawn.slick.SlickException: Error loading World
at main.java.TestGame.init(TestGame.java:39)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at main.java.Main.main(Main.java:40)
Caused by: java.lang.RuntimeException: Resource not found: tileset.png
at org.newdawn.slick.util.ResourceLoader.getResourceAsStream(ResourceLoader.java:69)
at org.newdawn.slick.opengl.InternalTextureLoader.getTexture(InternalTextureLoader.java:169)
at org.newdawn.slick.Image.<init>(Image.java:196)
at org.newdawn.slick.SpriteSheet.<init>(SpriteSheet.java:129)
at org.newdawn.slick.SpriteSheet.<init>(SpriteSheet.java:115)
at org.newdawn.slick.SpriteSheet.<init>(SpriteSheet.java:102)
at main.java.Resources.addTileSet(Resources.java:55)
at main.java.JSONLoader.load(JSONLoader.java:33)
at main.java.TestGame.init(TestGame.java:31)
... 3 more
我彻底浏览了一下,我不知道我应该把tileset.png放在哪里,或者为什么这个世界不会为此加载。您是否创建了自己的.json导出世界来测试它?
答案 0 :(得分:2)
非常接近。不幸的是Inflater
比这更复杂:
而不是
Decoder decoder = Base64.getDecoder();
byte[] decodedBytes = decoder.decode((byte[]) layer.get("data"));
Inflater decompresser = new Inflater();
solids = parse((JSONArray)decompresser.inflate(decodedBytes));
试试这个:
Object o = layer.get("data");
if ( o instanceof JSONArray )
solids = parseJSONArray( (JSONArray) o );
else if ( o instanceof String
&& "base64".equals( layer.get( "encoding" ) )
&& "zlib".equals( layer.get( "compression" ) )
)
{
// base64 decode
Decoder decoder = Base64.getDecoder();
byte[] decodedBytes = decoder.decode((String) o);
// zlib decompress
Inflater decompresser = new Inflater();
decompresser.setInput(decodedBytes);
byte[] decompressedBytes = new byte[WIDTH * HEIGHT * 4];
int resultLength = decompresser.inflate(decompressedBytes);
decompresser.end();
// convert `byte[N*4]` to `int[N]`
int[] result = new int[WIDTH * HEIGHT];
ByteBuffer bb = ByteBuffer.wrap(decompressedBytes);
bb.order(ByteOrder.LITTLE_ENDIAN);
bb.asIntBuffer().get(result);
solids = parseIntArray( result );
}
else
throw new RuntimeException( "Unimplemented data type: "
+ o.getClass().getName() );
将原始parse
函数重命名为parseJSONArray
(例如),
然后将其复制到新功能parseByteArray
,如下所示:
private static Image[][] parseIntArray( int[] array )
{
Image[][] layer = new Image[WIDTH][HEIGHT];
for (int x = 0; x < WIDTH; ++x)
for (int y = 0; y < HEIGHT; ++y)
layer[x][y] = getSpriteImage( array[ y * WIDTH + x ] );
return layer;
}