【问题标题】:libgdx not initialized yetlibgdx 尚未初始化
【发布时间】:2016-04-25 01:07:10
【问题描述】:

我一直在编写我的 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

【问题讨论】:

  • 似乎在调用 gameUtils.init() 之前生成一个新的 box2d 世界可以解决问题

标签: java libgdx


【解决方案1】:

正如我在其他链接答案中所写的,请确保您的项目设置正确。 Box2D 是一个扩展,需要它自己的原生库才能运行。

这似乎是尚未加载的 Box2D 本机的特定问题。它们不会在 LibGDX 初始化时自动加载,但您必须自己触发(参见 wiki)。

您发布的代码看起来是正确的,但是在您可以使用任何与 Box2D 相关的东西之前,您必须调用 Box2D.init()。或者,创建一个 World 对象也是如此,但这不是最好的方法。

【讨论】:

    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 2021-06-21
    • 2020-06-17
    • 2015-07-24
    相关资源
    最近更新 更多