Java BodyEditorLoader-无此方法
因此,我开始使用物理体编辑器作为libgdx的扩展。我还使用Android Studio编译代码,这样我就可以在真实设备上运行应用程序。 但是,不幸的是,当我创建一个新的加载程序时,如下所示:Java BodyEditorLoader-无此方法,java,android,libgdx,box2d,Java,Android,Libgdx,Box2d,因此,我开始使用物理体编辑器作为libgdx的扩展。我还使用Android Studio编译代码,这样我就可以在真实设备上运行应用程序。 但是,不幸的是,当我创建一个新的加载程序时,如下所示: BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("tnkA.json")); logcat向我提供了以下错误: java.lang.NoSuchMethodError: No virtual method parse(L
BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("tnkA.json"));
logcat向我提供了以下错误:
java.lang.NoSuchMethodError: No virtual method parse(Ljava/lang/String;)Ljava/lang/Object;
显然,物理体编辑器有问题。
有办法解决这个问题吗?谢谢
有一个完整的错误:
02-12 13:39:15.406 2372-2387/com.tynibattles04.game.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 172
Process: com.tynibattles04.game.android, PID: 2372
java.lang.NoSuchMethodError: No virtual method parse(Ljava/lang/String;)Ljava/lang/Object;
in class Lcom/badlogic/gdx/utils/JsonReader;
or its super classes (declaration of 'com.badlogic.gdx.utils.JsonReader' appears
in /data/app/com.tynibattles04.game.android-1/base.apk)
at aurelienribon.bodyeditor.BodyEditorLoader.readJson(BodyEditorLoader.java:179)
at aurelienribon.bodyeditor.BodyEditorLoader.<init>(BodyEditorLoader.java:41)
at com.tynibattles04.game.TinyBattles.createBottle(TinyBattles.java:127)
at com.tynibattles04.game.TinyBattles.create(TinyBattles.java:74)
at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1511)
at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)
02-12 13:39:15.406 2372-2387/com.tynibattles04.game.android E/AndroidRuntime﹕ 致命异常:GL172线程
进程:com.tynibattles04.game.android,PID:2372
NoSuchMethodError:没有虚拟方法解析(Ljava/lang/String;)Ljava/lang/Object;
类内Lcom/badlogic/gdx/utils/JsonReader;
或者它的超类(出现'com.badlogic.gdx.utils.JsonReader'声明)
in/data/app/com.tynibattles04.game.android-1/base.apk)
在aurelianribon.bodyeditor.BodyEditorLoader.readJson(BodyEditorLoader.java:179)
BodyEditorLoader.(BodyEditorLoader.java:41)
位于com.tynibattles04.game.TinyBattles.createBattle(TinyBattles.java:127)
位于com.tynibattles04.game.TinyBattles.create(TinyBattles.java:74)
位于com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
位于android.opengl.GLSurfaceView$GLThread.guarderun(GLSurfaceView.java:1511)
位于android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)
看,这可能对任何事情都有帮助:
package aurelianribon.bodyeditor;
导入com.badlogic.gdx.files.FileHandle;
导入com.badlogic.gdx.math.Vector2;
导入com.badlogic.gdx.physics.box2d.Body;
导入com.badlogic.gdx.physics.box2d.CircleShape;
导入com.badlogic.gdx.physics.box2d.FixtureDef;
导入com.badlogic.gdx.physics.box2d.PolygonShape;
导入com.badlogic.gdx.utils.Array;
导入com.badlogic.gdx.utils.JsonReader;
导入com.badlogic.gdx.utils.JsonValue;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
/**
*加载使用“物理实体编辑器”定义的碰撞装置
*应用程序。您只需要给它一个主体和相应的夹具
*名称,它会将这些固定装置连接到您的身体上。
*
*@作者Aurelian Ribon |http://www.aurelienribon.com
*/
公共类BodyEditorLoader{
//模型
私有最终模型;
//可重复使用的材料
私有最终列表向量池=新ArrayList();
private final PolygonShape PolygonShape=新PolygonShape();
私有最终CircleShape CircleShape=新CircleShape();
私有最终向量2 vec=新向量2();
// -------------------------------------------------------------------------
//演员
// -------------------------------------------------------------------------
公共BodyEditorLoader(文件句柄文件){
如果(file==null)抛出新的NullPointerException(“file为null”);
model=readJson(file.readString());
}
公共BodyEditorLoader(字符串str){
如果(str==null)抛出新的NullPointerException(“str为null”);
model=readJson(str);
}
// -------------------------------------------------------------------------
//公共API
// -------------------------------------------------------------------------
/**
*创建并应用编辑器中定义的装置。名称
*参数用于从加载的文件中检索正确的夹具。
*
*
*默认情况下,主体参照点(工具中的红十字)为
*位于图像的左下角。此参考点
*将放置在BodyDef位置点的正上方。因此,您应该
*小心地放置此参考点,以使您的身体处于最佳位置
*世界很容易与它的身体定义。位置点。请注意,以绘制图像
*在你身体的位置,你需要知道这个参考点
*(参见{@link#getOrigin(java.lang.String,float)}。
*
*
*此外,保存的形状也会被规格化
*图像被视为始终为1米。因此,您需要提供
*比例因子,以便根据需要调整多边形的大小(而不是
*我猜在你的游戏中每个人都有1米大)。
*
*@param body要将夹具连接到的Box2d body。
*@param name要加载的设备的名称。
*@param fd指定要应用于创建的车身固定装置的固定装置参数。
*@param scale所需的主体比例。默认宽度为1。
*/
public void attachFixture(主体、字符串名称、FixtureDef fd、浮动比例){
RigidBodyModel rbModel=model.rigidBodies.get(名称);
如果(rbModel==null)抛出新的RuntimeException(“未找到名称“+”名称“);
向量2原点=向量集(rbModel.origin).mul(比例);
对于(int i=0,n=rbModel.polygons.size();使用scl(比例)代替mul(比例)方法;)
package aurelienribon.bodyeditor;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Loads the collision fixtures defined with the Physics Body Editor
* application. You only need to give it a body and the corresponding fixture
* name, and it will attach these fixtures to your body.
*
* @author Aurelien Ribon | http://www.aurelienribon.com
*/
public class BodyEditorLoader {
// Model
private final Model model;
// Reusable stuff
private final List<Vector2> vectorPool = new ArrayList<Vector2>();
private final PolygonShape polygonShape = new PolygonShape();
private final CircleShape circleShape = new CircleShape();
private final Vector2 vec = new Vector2();
// -------------------------------------------------------------------------
// Ctors
// -------------------------------------------------------------------------
public BodyEditorLoader(FileHandle file) {
if (file == null) throw new NullPointerException("file is null");
model = readJson(file.readString());
}
public BodyEditorLoader(String str) {
if (str == null) throw new NullPointerException("str is null");
model = readJson(str);
}
// -------------------------------------------------------------------------
// Public API
// -------------------------------------------------------------------------
/**
* Creates and applies the fixtures defined in the editor. The name
* parameter is used to retrieve the right fixture from the loaded file.
* <br/><br/>
*
* The body reference point (the red cross in the tool) is by default
* located at the bottom left corner of the image. This reference point
* will be put right over the BodyDef position point. Therefore, you should
* place this reference point carefully to let you place your body in your
* world easily with its BodyDef.position point. Note that to draw an image
* at the position of your body, you will need to know this reference point
* (see {@link #getOrigin(java.lang.String, float)}.
* <br/><br/>
*
* Also, saved shapes are normalized. As shown in the tool, the width of
* the image is considered to be always 1 meter. Thus, you need to provide
* a scale factor so the polygons get resized according to your needs (not
* every body is 1 meter large in your game, I guess).
*
* @param body The Box2d body you want to attach the fixture to.
* @param name The name of the fixture you want to load.
* @param fd The fixture parameters to apply to the created body fixture.
* @param scale The desired scale of the body. The default width is 1.
*/
public void attachFixture(Body body, String name, FixtureDef fd, float scale) {
RigidBodyModel rbModel = model.rigidBodies.get(name);
if (rbModel == null) throw new RuntimeException("Name '" + name + "' was not found.");
Vector2 origin = vec.set(rbModel.origin).mul(scale);
for (int i=0, n=rbModel.polygons.size(); i<n; i++) {
PolygonModel polygon = rbModel.polygons.get(i);
Vector2[] vertices = polygon.buffer;
for (int ii=0, nn=vertices.length; ii<nn; ii++) {
vertices[ii] = newVec().set(polygon.vertices.get(ii)).mul(scale);
vertices[ii].sub(origin);
}
polygonShape.set(vertices);
fd.shape = polygonShape;
body.createFixture(fd);
for (int ii=0, nn=vertices.length; ii<nn; ii++) {
free(vertices[ii]);
}
}
for (int i=0, n=rbModel.circles.size(); i<n; i++) {
CircleModel circle = rbModel.circles.get(i);
Vector2 center = newVec().set(circle.center).mul(scale);
float radius = circle.radius * scale;
circleShape.setPosition(center);
circleShape.setRadius(radius);
fd.shape = circleShape;
body.createFixture(fd);
free(center);
}
}
/**
* Gets the image path attached to the given name.
*/
public String getImagePath(String name) {
RigidBodyModel rbModel = model.rigidBodies.get(name);
if (rbModel == null) throw new RuntimeException("Name '" + name + "' was not found.");
return rbModel.imagePath;
}
/**
* Gets the origin point attached to the given name. Since the point is
* normalized in [0,1] coordinates, it needs to be scaled to your body
* size. Warning: this method returns the same Vector2 object each time, so
* copy it if you need it for later use.
*/
public Vector2 getOrigin(String name, float scale) {
RigidBodyModel rbModel = model.rigidBodies.get(name);
if (rbModel == null)
throw new RuntimeException("Name '" + name + "' was not found.");
return vec.set(rbModel.origin).scl(scale);
}
/**
* <b>For advanced users only.</b> Lets you access the internal model of
* this loader and modify it. Be aware that any modification is permanent
* and that you should really know what you are doing.
*/
public Model getInternalModel() {
return model;
}
// -------------------------------------------------------------------------
// Json Models
// -------------------------------------------------------------------------
public static class Model {
public final Map<String, RigidBodyModel> rigidBodies = new HashMap<String, RigidBodyModel>();
}
public static class RigidBodyModel {
public String name;
public String imagePath;
public final Vector2 origin = new Vector2();
public final List<PolygonModel> polygons = new ArrayList<PolygonModel>();
public final List<CircleModel> circles = new ArrayList<CircleModel>();
}
public static class PolygonModel {
public final List<Vector2> vertices = new ArrayList<Vector2>();
private Vector2[] buffer; // used to avoid allocation in attachFixture()
}
public static class CircleModel {
public final Vector2 center = new Vector2();
public float radius;
}
// -------------------------------------------------------------------------
// Json reading process
// -------------------------------------------------------------------------
private Model readJson(String str) {
Model m = new Model();
JsonValue map = new JsonReader().parse(str);
JsonValue bodyElem = map.getChild("rigidBodies");
for (; bodyElem != null; bodyElem = bodyElem.next()) {
RigidBodyModel rbModel = readRigidBody(bodyElem);
m.rigidBodies.put(rbModel.name, rbModel);
}
return m;
}
private RigidBodyModel readRigidBody(JsonValue bodyElem) {
RigidBodyModel rbModel = new RigidBodyModel();
rbModel.name = bodyElem.getString("name");
rbModel.imagePath = bodyElem.getString("imagePath");
JsonValue originElem = bodyElem.get("origin");
rbModel.origin.x = originElem.getFloat("x");
rbModel.origin.y = originElem.getFloat("y");
// polygons
JsonValue polygonsElem = bodyElem.getChild("polygons");
for (; polygonsElem != null ;polygonsElem = polygonsElem.next()){
PolygonModel polygon = new PolygonModel();
rbModel.polygons.add(polygon);
JsonValue vertexElem = polygonsElem.child();
for (; vertexElem != null; vertexElem = vertexElem.next()) {
float x = vertexElem.getFloat("x");
float y = vertexElem.getFloat("y");
polygon.vertices.add(new Vector2(x, y));
}
polygon.buffer = new Vector2[polygon.vertices.size()];
}
// circles
JsonValue circleElem = bodyElem.getChild("circles");
for (; circleElem != null; circleElem = circleElem.next()) {
CircleModel circle = new CircleModel();
rbModel.circles.add(circle);
circle.center.x = circleElem.getFloat("cx");
circle.center.y = circleElem.getFloat("cy");
circle.radius = circleElem.getFloat("r");
}
return rbModel;
}
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
private Vector2 newVec() {
return vectorPool.isEmpty() ? new Vector2() : vectorPool.remove(0);
}
private void free(Vector2 v) {
vectorPool.add(v);
}
}