从android中的另一个类加载纹理



我正在为android开发一款游戏,使用libgdx和IntelliJ。在这个游戏中,我有两个"屏幕",这些屏幕的背景使用相同的纹理,这就是我加载它们的方式:

    Texture backgroundTexture;
    public static Sprite backgroundSprite;
    backgroundTexture = new Texture("textures/background.png");
    backgroundSprite = new Sprite(backgroundTexture);

这在两个屏幕中都完成了,所以我的问题是,我可以在另一个类中加载这些纹理,然后以某种方式在两个屏中使用它们吗?如果我走在正确的轨道上,应该如何实施?

您可以以自己的方式使用AssetLoader加载资产。它并没有什么特别的,只是一个类,它在应用程序启动时被调用,因为事实是,当一切都在运行时,您应该避免加载资产。这只是一个简单的类,包含静态的东西。

public class AssetLoader {
    public static Texture myBackgroundTexture;
    public static void Load() {
        myBackgroundTexture = new Texture("mybgs/my_bg_texture.png");
    }

让我们在应用程序启动时调用AssetLoader.Load(),您可以在任何地方引用您的东西,例如:

Texture thisScreenBg = AssetLoader.myBackgroundTexture;

最新更新