gpt4 book ai didi

java - Libgdx:当 AssetManager.Update() 返回 true 时无法转换纹理

转载 作者:行者123 更新时间:2023-12-01 09:07:42 29 4
gpt4 key购买 nike

我正在学习如何使用 libgdx,但我在此处或文档中都没有找到对此的 awsnser。我的问题在标题中已经说得很清楚了。当我尝试在 5 秒前加载纹理时(这是因为我使用 Gdx 计时器,仅用于测试),应用程序崩溃并给出此信息

Exception in thread "LWJGL Application" java.lang.ClassCastException: com.badlogic.gdx.graphics.Texture cannot be cast to com.badlogic.gdx.scenes.scene2d.ui.Image
at com.hoticecream.mad.Background.setStaticBackground(Background.java:33)
at com.hoticecream.mad.Playgorund$1.run(Playgorund.java:102)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.executeRunnables(LwjglApplication.java:257)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:208)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)

由于时间短,我将发布整个类(class)

 class Background {

private Image background;
private final String backgroundName;
private AssetManager manager;

Background(AssetManager manager){

this.manager = manager;

background = new Image();

backgroundName = "background1.png";
manager.load(backgroundName, Texture.class);
manager.finishLoadingAsset(backgroundName);



}

public void setStaticBackground(Stage stage){


//here is where error happends, affter manager.update() returning true
if(manager.update()){
background = manager.get(backgroundName);

}else {
Gdx.app.log("Manager","not loaded");
}

background.setHeight(stage.getHeight());
background.setWidth(stage.getWidth());

stage.addActor(background);
}

public void dispose(){

manager.unload(backgroundName);

}

}

幻灯片注释: Assets 管理器通过内部类的参数传递

最佳答案

显然,您从管理器获取的对象(它是纹理,因为设置的加载器类型 - manager.load(backgroundName, Texture.class);)不可转换为 图像类型。

尝试从纹理创建一个 Image 对象:

...
if(manager.update()){
background = new Image((Texture) manager.get(backgroundName));
...

关于java - Libgdx:当 AssetManager.Update() 返回 true 时无法转换纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41131699/

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