2

我正在使用与本教程中使用的完全相同的代码,但由于某种原因,我在尝试运行项目时遇到错误。LibGDX 的默认项目运行良好。

错误:

Exception in thread "main" java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.World.newWorld(FFZ)J
at com.badlogic.gdx.physics.box2d.World.newWorld(Native Method)
at com.badlogic.gdx.physics.box2d.World.<init>(World.java:222)
at net.ocps.tchs.permarun.PermaRun.<init>(PermaRun.java:19)
at net.ocps.tchs.permarun.Main.main(Main.java:14)

它所指的行(类中的第一行):

World world = new World(new Vector2(0, -100), true); 

编辑:在进一步调试后,我注释掉了涉及world变量的任何内容并且程序运行(除了一个黑框之外什么都没有显示,因为我在注释掉world定义行时注释掉了任何错误。)

Edit2:完整的代码有效。(有不同的使用方式,GdxNativesLoader.load();但这是我决定使用的方式,感谢 aquaraga)

import com.badlogic.gdx.ApplicationListener;  
import com.badlogic.gdx.Gdx;  
import com.badlogic.gdx.graphics.GL10;  
import com.badlogic.gdx.graphics.OrthographicCamera;  
import com.badlogic.gdx.math.Vector2;  
import com.badlogic.gdx.physics.box2d.Body;  
import com.badlogic.gdx.physics.box2d.BodyDef;  
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;  
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;  
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.physics.box2d.World;  
import com.badlogic.gdx.utils.GdxNativesLoader;
public class PermaRun implements ApplicationListener {  
     World world;  
     Box2DDebugRenderer debugRenderer;  
     OrthographicCamera camera;  
     static final float BOX_STEP=1/60f;  
     static final int BOX_VELOCITY_ITERATIONS=6;  
     static final int BOX_POSITION_ITERATIONS=2;  
     static final float WORLD_TO_BOX=0.01f;  
     static final float BOX_WORLD_TO=100f;  
     @Override  
     public void create() {
          GdxNativesLoader.load();
          world = new World(new Vector2(0.0f, -100.0f), true);
          camera = new OrthographicCamera();  
          camera.viewportHeight = 320;  
          camera.viewportWidth = 480;  
          camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);  
          camera.update();  
          //Ground body  
          BodyDef groundBodyDef =new BodyDef();  
          groundBodyDef.position.set(new Vector2(0, 10));  
          Body groundBody = world.createBody(groundBodyDef);  
          PolygonShape groundBox = new PolygonShape();  
          groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);  
          groundBody.createFixture(groundBox, 0.0f);  
          //Dynamic Body  
          BodyDef bodyDef = new BodyDef();  
          bodyDef.type = BodyType.DynamicBody;  
          bodyDef.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2);  
          Body body = world.createBody(bodyDef);  
          CircleShape dynamicCircle = new CircleShape();  
          dynamicCircle.setRadius(5f);  
          FixtureDef fixtureDef = new FixtureDef();  
          fixtureDef.shape = dynamicCircle;  
          fixtureDef.density = 1.0f;  
          fixtureDef.friction = 0.0f;  
          fixtureDef.restitution = 1;  
          body.createFixture(fixtureDef);  
          debugRenderer = new Box2DDebugRenderer();  
     }  
     @Override  
     public void dispose() {  
     }  
     @Override  
     public void render() {            
          Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
          debugRenderer.render(world, camera.combined);  
          world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);  
     }  
     @Override  
     public void resize(int width, int height) {  
     }  
     @Override  
     public void pause() {  
     }  
     @Override  
     public void resume() {  
     }  

}
4

1 回答 1

7

你可能想试试GdxNativesLoader.load();。以静态方式执行,即当 JVM 加载类时:

static {
    GdxNativesLoader.load();
}

显然,当您的代码或 java 库调用一些本机代码时,会发生链接错误。

于 2013-08-28T18:40:40.883 回答