简体   繁体   English

libGDX我应该在哪里使用dispose /如何使对象可抛弃?

[英]libGDX Where should i use dispose / how to make an object disposeable?

I would like to know: 我想知道:

  1. I have a Stage and it holds an Actor class. 我有一个舞台,这里有一个演员班。 Do I have to disopse the Texture and Sprite? 我是否必须取消纹理和精灵?

     public class SimpleImage extends Image { protected Sprite sprite; public SimpleImage(FileHandle file) { super(); sprite = new Sprite(new Texture(file)); } @Override public void draw(Batch batch, float parentAlpha) { super.draw(batch, parentAlpha); sprite.draw(batch); batch.setColor(this.getColor()); } @Override public void act(float delta) { super.act(delta); } @Override protected void positionChanged() { super.positionChanged(); sprite.setPosition(getX(), getY()); } } 
  2. How to make obj disposable? 如何使obj一次性使用? In the Java libs are two: MediaDisposer.Disposable and MediaDisposer.ResourceDisposer. 在Java中,有两个库:MediaDisposer.Disposable和MediaDisposer.ResourceDisposer。

  3. When do I need to make a obj disposable? 我什么时候需要将obj丢弃?

You probably want to apply separation of concerns . 您可能希望将关注点分离 Your SimpleImage class should not have to care about managing assets. 您的SimpleImage类不必关心资产管理。 As such, you should not create the Texture in your SimpleImage class in the first place. 因此,首先不应在SimpleImage类中创建Texture And thus you shouldn't have to dispose it. 因此,您不必处置它。

Managing assets it usually done by using an AssetManager . 管理资产通常使用AssetManager完成

Also, you should pack your images into a single Texture . 另外,您应该将图像打包到单个Texture Meaning that you can't just use one Texture per SimpleImage , but instead would use one Texture for multiple SimpleImage s (or other classes that need an image). 这意味着您不能只为每个SimpleImage使用一个Texture ,而是要对多个SimpleImage使用一个Texture (或其他需要图像的类)。

So you should reconsider your approach. 因此,您应该重新考虑您的方法。 That said, let me answer your questions. 就是说,让我回答您的问题。

  1. You need to dispose everything that implements the Disposable interface. 您需要处理实现Disposable接口的所有内容。 Texture implements the Disposable interface, Sprite doesn't. Texture实现了Disposable接口,而Sprite没有。

  2. If you want to make your object implement the Disposable interface then make it implement the Disposable interface. 如果要使对象实现Disposable接口,则使它实现Disposable接口。 It's com.badlogic.gdx.utils.Disposable if you're having trouble importing it. 如果导入困难, com.badlogic.gdx.utils.Disposable It's a libGDX interface and has nothing to do with MediaDisposer. 这是一个libGDX界面 ,与MediaDisposer无关。

  3. You could make an object disposable when it is responsible for disposing resources. 当对象负责处理资源时,可以将其设置为一次性的。 Typically this means that is responsible for calling the .dispose() method on member objects it created. 通常,这意味着它负责在其创建的成员对象上调用.dispose()方法。 You don't have to make them disposable though, as long as you make sure that you dispose the resources you are creating, then it's fine. 不过,您不必使它们成为一次性的,只要您确保对正在创建的资源进行处理,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM