libgdx/assets什么算作“非静态"? [英] libgdx/assets What counts as "non static"?

查看:67
本文介绍了libgdx/assets什么算作“非静态"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我一直在努力使一些libgdx项目在Android上运行. 我已经读过AssetManager绝不应该声明为静态,因为这会导致暂停/恢复问题.

Recently i have been working to get some libgdx projects working on Android. I have read that AssetManager should never be declared static as this causes issues on pause/resume.

但是您到底能摆脱什么呢?

But what exactly can you get away with?

public  AssetManager assetsmanager;
static public  AssetManager assets;

private void setup() {

    assetsmanager = new AssetManager();
    assets=assetsmanager;
    ....

似乎很容易?

推荐答案

如果不将其设置为静态,则必须执行以下两项操作之一:

If you don't make it static, then you have to do one of two things:

  1. 创建AssetManager,然后在屏幕之间传递其引用
  1. Create the AssetManager and then pass a reference of it between your screens

  1. 在每个屏幕上创建一个新的AssetManager,并在创建该屏幕时仅为每个屏幕加载相关资产.
  1. Create a new AssetManager each screen and load it up with only the relevant assets for each screen when the screen is created.

我更喜欢在您的主类中创建一个AssetManager并加载所有资产.将您的Main类的引用传递到其他屏幕,以便我可以在那里访问Assetmanager.

I prefer to create one AssetManager in your main class and load all assets. Pass reference of your Main class to other screen so that I can access assetmanager there.

public class Main extends Game {

   public AssetManager assetManager;  // for loading all assets

   @Override
   public void create(){
        assetManager = new AssetManager();
        assetManager.load("assets/data/yourSkin", Skin.class);
        assetManager.finishLoading();  // load assets (not asynchron for this example)
        setScreen(new GameScreen(this));
   }

   @Override
   public void dispose() {
      assetManager.dispose();   // disposes all assets when the game exits
   }
}

Gdx.app.getApplicationListener()返回ApplicationListener实例.因此您可以强制转换到已实现的类,然后轻松访问该类的任何方法或数据成员.

Gdx.app.getApplicationListener() return ApplicationListener instance. so you can typecast to your implemented class and then easily access any method or data member of that class.

通过这种方式:

((Main)Gdx.app.getApplicationListener()).assetManager  // <-- You can use from where you want

这篇关于libgdx/assets什么算作“非静态"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆