问题描述
最近,我一直在努力使一些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:
- 创建
AssetManager
,然后在屏幕之间传递其引用
- Create the
AssetManager
and then pass a reference of it between your screens
或
- 在每个屏幕上创建一个新的
AssetManager
,并在创建该屏幕时仅为每个屏幕加载相关资产.
- 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什么算作“非静态"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!