简体   繁体   中英

Problems loading assets in libgdx

I'm new to the world of app development, but I already have some specific knowledge. But my issue is the following involving libgdx and Android Studio:

When trying to load a texture through the Assets Manager method, the following error occurs: `

E/AndroidRuntime: FATAL EXCEPTION: GLThread 593
    Process: br.com.tpgames.dbz, PID: 19495
    com.badlogic.gdx.utils.GdxRuntimeException: Asset not loaded: bater_1/goku2.png
        at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:172)
        at com.badlogic.gdx.assets.AssetManager.get(AssetManager.java:143)
        at br.com.tpgames.dbz.SplashScreen.<init>(SplashScreen.java:53)

`

My codes:

main class:

`

public class MainClass extends Game {

   public AssetManager manager;


   public void create () {




      setScreen(new SplashScreen (this, manager));

   }


}

`

Splash screen `

public class SplashScreen implements Screen {

    private Game game;
    private AssetManager manager;
    private float time = 0;


    private SpriteBatch batch;
    private Texture tex;

    long startime;

    public Texture goku;



    public SplashScreen(Game game, AssetManager manager){
        this.game = game;
        this.manager = manager;

        batch = new SpriteBatch();
        tex = new Texture("logo.png");

        startime = TimeUtils.millis();

        manager = new AssetManager();
        for (int i=1;i<=5;i++){
            manager.load("bater_1/"+i+".png",Texture.class);
        }

        boolean load = manager.isLoaded("bater_1/goku1.png");
        Gdx.app.log("Log", String.valueOf(load));


        goku = manager.get("bater_1/goku2.png",Texture.class);




    }


    @Override
    public void show() {

    }

    @Override
    public void render(float delta) {
        time+=delta;


        if (manager.update() && time >=2){      //Se o tempo for maior que 2 segundos, vai abrir a tela principal
            game.setScreen(new Tela_Principal(game,manager));
        }


        Gdx.gl.glClearColor(1,1,1,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(tex, (float) (screenx*0.26), (float) (screeny*0.1), (float) (screenx*0.5), (float) (screeny*0.8));

        batch.end();

    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }
}

`

Constants `

public class Constantes {
    public static int screenx = Gdx.graphics.getWidth();
    public static int screeny = Gdx.graphics.getHeight();
}

`



Main screen

` public class Tela_Principal implements Screen {

private Game game;
private AssetManager manager;
private SpriteBatch batch;

private SplashScreen splashScreen;

// private Texture goku;


public Tela_Principal(Game game, AssetManager manager){
    this.game = game;
    this.manager = manager;

    splashScreen = new SplashScreen(game,manager);


    for (int i=1;i<=5;i++){
        manager.load("bater_1/goku"+i+".png",Texture.class);
    }

    batch = new SpriteBatch();
    // goku = manager.get("bater_1/goku1.png",Texture.class);

}


@Override
public void show() {

}

@Override
public void render(float delta) {


    Gdx.gl.glClearColor(0,1,0,0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    batch.draw(splashScreen.goku, (float) (screenx *0.1), (float) (screeny*0.1));
    batch.end();

}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}

}

`

I thank you for your help!!

manager.load() will queue your asset, not immediately load it. Only when manager.update() returns true will the assets be available.

Looks like your problem is in SplashScreen

        for (int i=1;i<=5;i++){
            manager.load("bater_1/"+i+".png",Texture.class);
        }
        //not allowed
        goku = manager.get("bater_1/goku2.png",Texture.class);

AssetManager is well explained here https://libgdx.com/wiki/managing-your-assets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM