gpt4 book ai didi

java - 使用 AssetsManager 在 Java libgdx 上出错

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

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

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 Thank you for the tip

所以在我输入“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)

编辑2

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

所以它已经解决了,我把

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

这样,当屏幕加载时,他会在开始时加载资源,然后我将其从

"public void render(){" 

因为他总是加载资源,因为它是一个循环(我认为是这样),然后

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

感谢 Rc 的提示。然后在同一个地方我放了

"assetsmanager.manager.finishLoading();"

因此它会在执行某些操作之前加载所有资源。谢谢 spy 的提示。

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

最佳答案

您应该注意,当您加载资源时,在调用 finishLoading 之前您无法使用它们。 finishLoading 会阻止所有尝试获取资源的调用,直到它们完全加载到内存中。

我强烈建议您阅读 AssetManager API .

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

关于java - 使用 AssetsManager 在 Java libgdx 上出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38428887/

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