gpt4 book ai didi

java - libgdx 尚未初始化

转载 作者:行者123 更新时间:2023-11-30 10:45:51 26 4
gpt4 key购买 nike

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

异常(exception):

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 中提到的线如

Another problem might be that you instantiate a SpriteBatch (or something else which internally uses a SpriteBatch) too early (looked a bit like this in the stacktrace).

但正如答案中提到的

Instead, create such things in the create/show methods of your game.

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

我的代码如下:(我已经去掉了e相关的方法)

应用类

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

最佳答案

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

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

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

关于java - libgdx 尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36830685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com