【发布时间】:2016-06-09 05:36:05
【问题描述】:
我对 java 有相当多的了解,但对 Slick2d 和 JSON 还是陌生的。我无法弄清楚为什么在尝试加载 .json 地图文件时会引发异常。我正在使用 eclipse、slick2d、lwjgl 和 json。我建立了一个地图文件,它位于 eclipse 下 res/maps/ 这是堆栈跟踪
java.lang.ClassCastException: java.lang.String cannot be cast to org.json.simple.JSONArray
at cardboard.world.World.load(World.java:33)
at cardboard.Engine.initStatesList(Engine.java:49)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:393)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:317)
at cardboard.Engine.main(Engine.java:31)
这是执行代码
try {
World.load("res/maps/world.json");
} catch (Exception e) {
System.err.println("Map does not exist");
System.exit(0);
}
这是世界级的
import java.io.FileReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.newdawn.slick.Image;
import org.newdawn.slick.SpriteSheet;
import cardboard.Resources;
public class World {
public static Image[][] solids;
public static int WIDTH;
public static int HEIGHT;
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")) {
solids = parse((JSONArray)layer.get("data"));
} else if (type.equals("spawns")) {
// Spawn point code
}
}
}
private static Image[][] parse(JSONArray arr) {
Image[][] layer = new Image [WIDTH][HEIGHT];
int index;
for (int y = 0; y < WIDTH; y++) {
for (int x = 0; x < HEIGHT; x++) {
index = (int)((long)arr.get((y * WIDTH) + x));
layer[x][y] = getSpriteImage(index);
}
}
return layer;
}
private static Image getSpriteImage(int index) {
if (index == 0) return null;
index -= 1;
SpriteSheet sheet = Resources.getSprite("tileset");
//int vertical = sheet.getVerticalCount();
int horizontal = sheet.getHorizontalCount();
int y = (index / horizontal); //vert?
int x = (index % horizontal);
return sheet.getSubImage(x, y);
}
}
Eclipse 没有抛出任何错误我只是收到我写的错误消息“地图不存在”如果你能看一下,那就太棒了!谢谢
如果你需要 Resources.java
package cardboard;
import java.util.HashMap;
import java.util.Map;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.SpriteSheet;
import cardboard.world.Tile;
public class Resources {
private static Map<String, Image> images;
private static Map<String, SpriteSheet> sprites;
private static Map<String, Sound> sounds;
public Resources() {
images = new HashMap<String, Image>();
sprites = new HashMap<String, SpriteSheet>();
sounds = new HashMap<String, Sound>();
try {
sprites.put("tileset", loadSprite("res/tileset.png", Tile.SMALL_SIZE, Tile.SMALL_SIZE));
} catch (SlickException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Image loadImage(String path) throws SlickException {
return new Image(path, false, Image.FILTER_NEAREST);
}
public static SpriteSheet loadSprite(String path, int tw, int th) throws SlickException {
return new SpriteSheet(loadImage(path), tw, th);
}
public static SpriteSheet getSprite(String getter) {
return sprites.get(getter);
}
public static Image getSpriteImage(String getter, int x, int y) {
return sprites.get(getter).getSubImage(x,y);
}
public static Image image(String getter) {
return sprites.get(getter);
}
public static Image getImage(String getter) {
return images.get(getter);
}
public static Sound getSound(String getter) {
return sounds.get(getter);
}
}
【问题讨论】:
-
你得到了什么异常?例如用
System.out.println打印它 -
只打印出异常
-
请在您的问题中添加打印堆栈。 @squirt706 谢谢
-
您知道相当多的 Java,但您不知道使用
e.printStackTrace();显示异常堆栈跟踪的绝对基础知识? -
@Enigo 你不要使用
System.out.println来显示异常。使用它来显示NullPointerException将显示null,这与他当前的异常“处理”一样有用。
标签: java json eclipse lwjgl slick