libgdx尚未初始化

时间:2016-04-25 01:07:11

标签: java libgdx

我已经编写了我的libgdx应用程序(仅适用于桌面)一段时间,并且在决定按部分清理代码后,我遇到了一个问题,我似乎无法解决自己..

异常:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape()J
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
Caused by: java.lang.UnsatisfiedLinkError: com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape()J
    at com.badlogic.gdx.physics.box2d.PolygonShape.newPolygonShape(Native Method)
    at com.badlogic.gdx.physics.box2d.PolygonShape.<init>(PolygonShape.java:29)
    at com.mygdx.game.handler.BodyEditorLoader.<init>(BodyEditorLoader.java:41)
    at com.mygdx.game.util.GameUtils.init(GameUtils.java:23)
    at com.mygdx.game.DungeonLife.create(DungeonLife.java:168)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
谷歌搜索了一段时间后,我认为我的错误是this线程中提到的 如在

  

另一个问题可能是你过早地实例化一个SpriteBatch(或其他内部使用SpriteBatch的东西)(在堆栈跟踪中看起来有点像这样)。

但正如答案所提到的那样

  

相反,在游戏的创建/显示方法中创建这样的东西。

我似乎无法理解libgdx何时被初始化并准备好使用,以及放置我的GameUtils.init()方法以确保libgdx被初始化

我的代码如下:(我已采用电子相关方法)

申请类

package com.mygdx.game;

import box2dLight.RayHandler;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.MapProperties;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.mygdx.game.entity.MobEntity;
import com.mygdx.game.entity.PhysicalEntity;
import com.mygdx.game.entity.Player;
import com.mygdx.game.entity.Weapon;
import com.mygdx.game.handler.*;
import com.mygdx.game.util.CollisionConstants;
import com.mygdx.game.util.GameUtils;
import com.mygdx.game.util.TileObjectUtil;
import com.mygdx.game.util.WorldConstants;
import com.mygdx.game.valtype.WeaponDefinition;

public class DungeonLife extends ApplicationAdapter implements WorldConstants {

    OrthographicCamera camera;

    float width , height;

    Texture texture;
    TextureRegion[] enttex;

    //TEMP
    MobEntity demo;

    Player thePlayer;
    PlayerInputProcessor playerInputProcessor;

    ScreenUI ui;

    int mapWidth , mapHeight;

    //========================================
    GameMap gameMap;
    //========================================


    @Override
    public void create () {

        texture = new Texture("maps/img/tileset_entity.png");

        enttex = new TextureRegion[(int) ((texture.getWidth()*texture.getHeight()) / (BLOCK*BLOCK))];

        enttex[0] = new TextureRegion(texture , 0 , 0 , (int)BLOCK , (int)BLOCK);
        enttex[1] = new TextureRegion(texture , (int)BLOCK , 0 , (int)BLOCK , (int)BLOCK);

        width = Gdx.graphics.getWidth()/5;
        height = Gdx.graphics.getHeight()/5;
        camera = new OrthographicCamera(width,height);
        camera.position.set(width / 2, height / 2, 0);

        camera.update();

        GameUtils.init(); // <------this guy

        //init();


    } ...

GameUtils

package com.mygdx.game.util;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.World;
import com.mygdx.game.entity.PhysicalEntity;
import com.mygdx.game.handler.*;

import java.util.PriorityQueue;

public class GameUtils {

    public static void init(){

        weapon_bodyEditorLoader = new BodyEditorLoader(Gdx.files.internal("textures/weapons/dungeonlife_weapons.json"));
        resourceManager = new ResourceManager();
        resourceManager.addRes("friendlyhealth" , new Texture("textures/ui/friendlyhealth.png"));
        resourceManager.addRes("enemyhealth" , new Texture("textures/ui/enemyhealth.png"));
        tmxMapLoader = new TmxMapLoader();
        gameContactListender = new GameContactListender();


    }


    //GLOBAL
    public static BodyEditorLoader weapon_bodyEditorLoader;
    public static GameContactListender gameContactListender;
    public static ResourceManager resourceManager;
    public static TmxMapLoader tmxMapLoader;


    //CURRENTS ============================

    public static GameMap CURRENT_GAMEMAP;

}

桌面启动器 (通常)

package com.mygdx.game.desktop;

import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.mygdx.game.DungeonLife;
import com.mygdx.game.util.GameUtils;

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.useGL30 = false;
        config.width=640;
        config.height=360;
        DungeonLife dungeonLife = new DungeonLife();
        new LwjglApplication(dungeonLife, config);
    }
}

帮助很多! :d

1 个答案:

答案 0 :(得分:1)

As I've already wrote in the other linked answer, make sure that your project is correctly set up. Box2D is an extension and needs its own native libraries to be able to run.

It seems that it's a specific problem with the Box2D natives which are not loaded yet. They are not loaded automatically when LibGDX is initializing, but you have to trigger that yourself (see wiki).

The code you've posted looks correct, but before you can use anything Box2D related, you have to call Box2D.init(). Alternatively, creating a World object does the same, but isn't the nicest way to do it.