2016-04-25 169 views
2

我一直在編碼我libgdx應用程序(僅適用於臺式機)的同時,並通過部分決定清理代碼部分iv'e得到了一個問題後,我似乎無法來解決自己..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) 

google搜索了一會兒後,我想通是在this線程 在

提到我的錯誤的另一個問題可能是你會過早實例化一個SpriteBatch(或其他內部使用SpriteBatch的東西)(在堆棧跟蹤中看起來有點像這樣)。

但作爲答案提到

相反,建立在你的遊戲的創建/顯示方法,這樣的事情。

我似乎無法理解當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); 
    } 
} 

幫助是大大appriciated! :D

+0

似乎在調用gameUtils.init()之前生成一個新的box2d世界可以解決問題,原因是某種原因 – kw7

回答

1

正如我已經寫在其他鏈接的答案,請確保您的項目設置正確。 Box2D是一個擴展,需要自己的本地庫才能運行。

看來這是Box2D本地人尚未加載的特定問題。當LibGDX正在初始化時,它們不會自動加載,但您必須親自觸發它(請參閱wiki)。

您發佈的代碼看起來是正確的,但在您可以使用與Box2D相關的任何內容之前,您必須致電Box2D.init()。另外,創建一個World對象也是一樣,但不是最好的方法。