使用AssetsManager在Java libgdx上出错

时间:2016-07-18 05:01:27

标签: java libgdx assets

所以我开始尝试用libgdx在java中做一些游戏,我正在尝试使用AssetsManager。但它一直给出错误。我将把代码(我将尝试恢复代码)和输出。 游戏通过加载屏幕很好,但当它到达游戏屏幕时它停止并给出错误,我相信它是因为资产。 如果有人知道我为什么会收到此错误。

DesktopLauncher.java

public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        new LwjglApplication(new pressme(), config);
    }
}

pressme.java

public class pressme extends Game {

    LoadingScreen loadingscreen;
    AssetsManager assetsmanager;
    Assets assets;


    @Override
    public void create() {
        assets = new Assets();
        assetsmanager = new AssetsManager();
        assetsmanager.load();
        assets.other();
        loadingscreen = new LoadingScreen(this);
        setScreen(loadingscreen);       
    }
}

LoadingScreen.java

public class LoadingScreen implements Screen {

    Assets assets;
    AssetsManager assetsmanager;
    private final pressme game;
    public GameScreen game_screen;

    public LoadingScreen(final pressme game){

        assets = new Assets();
        assetsmanager = new AssetsManager();
        this.game = game;

    }
    @Override
    public void show() {
        assetsmanager.load();

    }

    private void update(float delta){

        System.out.println(progress);
        if(assetsmanager.manager.update()) {
            game.setScreen(new GameScreen(game));
          }
        progress = assetsmanager.manager.getProgress();     
    }

}

GameScreen.java

public class GameScreen implements Screen {
    pressme game;
    Assets assets;
    AssetsManager assetsmanager;

    public GameScreen(pressme game){
        this.game = game;
        assets = new Assets();
        assetsmanager = new AssetsManager();
    }
    @Override
    public void render(float delta) { 

    Gdx.gl.glClearColor( 0.5F, 0.5F, 0.5F, 0.5F);
        Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );


        camera.update();
        generalUpdate(touch, camera);

        batch.setProjectionMatrix(camera.combined);
        assets.load();
        batch.begin();
        batch.draw(assets.back, 0, 0);
        batch.end();
   }


 }

Assets.java

public class Assets {

    AssetsManager assetsmanager;

    public Sprite back;

    public void load(){
        assetsmanager = new AssetsManager();
        assetsmanager.load();
        back = new Sprite(assetsmanager.manager.get(assetsmanager.back, Texture.class));
    }
}

AssetsManager.java

public class AssetsManager {

    public AssetManager manager = new AssetManager();

    public String back  = "back.png";

    public void load(){

    manager.load(back, Texture.class);

    }
}

输出

LOADINGGG
0.0 

0.0 

0.015151516 

0.015151516 

0.030303031 

    .
    .
    .
0.969697 

0.9848485 

0.9848485 

Exception in thread "LWJGL Application" java.lang.NullPointerException

     at com.badlogic.gdx.graphics.g2d.SpriteBatch.draw(SpriteBatch.java:586)

     at com.david.pressme.GameScreen.render(GameScreen.java:244)

     at com.badlogic.gdx.Game.render(Game.java:46)

     at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)

     at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

修改

  

RC感谢您的提示

所以我把" assets.load() "仍然有一些错误。我不知道" assets.load() "它在正确的地方

LOADINGGG
0.0
0.0
0.015151516
0.015151516
0.030303031
.
.
.
0.969697
0.969697
0.9848485
0.9848485
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: back.png
    at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:144)
    at com.david.pressme.Assets.load(Assets.java:254)
    at com.david.pressme.GameScreen.render(GameScreen.java:172)
    at com.badlogic.gdx.Game.render(Game.java:46)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:223)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

EDIT2

RC,Spylot ty的提示,这就是你所说的

所以它解决了,我放了

"assets.load" on "public void show()" 

这样当屏幕加载时他会在开始时加载组件,然后从

中取出它
"public void render(){" 

因为他总是加载这些组件,因为它是一个循环(我想是这样),然后是

"Assets.java public load(){" i put "assetsmanager.load();"

感谢Rc的提示。

然后放在同一个地方
"assetsmanager.manager.finishLoading();"

所以它在做某事之前加载所有资产。谢谢spylot的提示。

感谢所有帮助或尝试提供帮助的人。

1 个答案:

答案 0 :(得分:2)

您应该知道,在加载资产时,在调用finishLoading之前不能使用它们。 finishLoading阻止所有试图抓住资产的调用,直到它们完全加载到内存中。

我强烈建议您阅读AssetManager API

简而言之,在每次使用load()调用之后,您应该使用manager.finishLoading()完成它