我对Java有一定的了解,但对Slick 2d和JSON还是个新手。我不知道为什么在尝试加载.jsonMap文件时会引发异常。我使用eclipse,slick 2d,lwjgl和json。我构建了一个map文件,它位于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没有抛出任何错误,我只是收到了我写的错误消息“Map不存在”,如果你能看一看就好了!谢啦,谢啦
如果您需要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);
}
}
型
2条答案
按热度按时间zlhcx6iw1#
在
java
中引入了两种异常。A)Checked Exception(Compile Time):-在编译时处理异常,只是为了检测任何外部组件或资源依赖(如
FileNotFound
、IOException
)。B)Unchecked Exception(Runtime Exception):-在任何逻辑错误或任何意外情况下出现此异常。(
IndexOutOfBound
,NullPointer
等)现在你的问题
为什么尝试加载.jsonMap文件时会引发异常?
如果你试图加载任何文件
.json
在你的程序,这是一个外部资源加载从任何存储区域(磁盘,云等)。因此,存储区域中可能不存在File
。因此,要处理这种不确定的情况,你的程序应该准备好处理这种情况(这不是逻辑错误,而是依赖于其他资源,而这些资源是你的代码的间接组成部分)。
这就是为什么要把这种东西放在
Checked Exception
段中。6yoyoihd2#
当你捕捉到异常
e
时,你应该对这个e
做些什么。若要获取有关引发异常的位置的详细信息,请使用以下代码:字符串