Android 如何在Libgdx中处理精灵?

Android 如何在Libgdx中处理精灵?,android,libgdx,Android,Libgdx,搜索之后,我发现Sprite是Libgdx中TextureRegion的一个子类。 那么如何处理精灵对象呢 public void create() { batch = new SpriteBatch(); sprite = new Sprite(new Texture("pizza.jpg")); } @Override public void render() { Gdx.gl.glClearColor(0,0,0,1); Gdx.gl.glClear(

搜索之后,我发现Sprite是Libgdx中TextureRegion的一个子类。 那么如何处理精灵对象呢

public void create() {
    batch = new SpriteBatch();
    sprite = new Sprite(new Texture("pizza.jpg"));


}

@Override
public void render() {
    Gdx.gl.glClearColor(0,0,0,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();

    batch.end();

}

@Override
public void dispose() {
    batch.dispose();
    sprite.getTexture().dispose();

这是正确的方法吗?提前谢谢

正如您所说,Sprite类本身没有dispose方法,因此您可以这样处理Sprite的纹理

但是,总体而言,更好的方法是使用加载纹理并对其进行处理。这样做允许您在一个位置管理所有纹理(因此,如果需要,可以在程序退出时一次性处理所有纹理)

下面是我目前正在制作的一个游戏的示例:

public class Controller extends Game {
    @Override
    public void create () {
        // Some loading stuff ...

        // Initialize the asset manager
        // TODO: Make a snazzy loading bar
        assetManager = new AssetManager();
        assetManager.load(ATLAS_NAME, TextureAtlas.class);
        assetManager.finishLoading();

        // Other loading stuff...
    }

    // Other methods snipped for space reasons...

    @Override
    public void dispose () {
        // I dispose of things not managed by the asset manager...

        // Dispose of any other resources
        assetManager.dispose();
    }

/**
     * Convenience method to safely load textures. If the texture isn't found, a blank one is created and the error is logged.
     * @param imageName The name of the image that is being looked up.
     * @return
     */
    public TextureRegionDrawable getManagedTexture(String imageName) {
        try {
            return new TextureRegionDrawable(assetManager.get(ATLAS_NAME, TextureAtlas.class).findRegion(imageName));
        } catch(Exception e) {
            Gdx.app.error(getClass().getSimpleName(), "Couldn't get managed texture.", e);
            return getEmptyTexture();
        }
    }
    public TextureRegionDrawable getEmptyTexture() {
        return new TextureRegionDrawable(new TextureRegion(new Texture(new Pixmap(1,1, Pixmap.Format.RGBA8888))));
    }
}
诚然,我创建了一个时髦的方法来包装asset manager,这样我总是可以得到一个纹理(即使它是空白的),但是asset manager的正常使用如下所示:

Texture tex = assetManager.get("my/texture/name.png", Texture.class);

它也适用于其他类,如地图集和皮肤。

正如您所说,Sprite类本身没有dispose方法,因此您可以这样处理Sprite的纹理

但是,总体而言,更好的方法是使用加载纹理并对其进行处理。这样做允许您在一个位置管理所有纹理(因此,如果需要,可以在程序退出时一次性处理所有纹理)

下面是我目前正在制作的一个游戏的示例:

public class Controller extends Game {
    @Override
    public void create () {
        // Some loading stuff ...

        // Initialize the asset manager
        // TODO: Make a snazzy loading bar
        assetManager = new AssetManager();
        assetManager.load(ATLAS_NAME, TextureAtlas.class);
        assetManager.finishLoading();

        // Other loading stuff...
    }

    // Other methods snipped for space reasons...

    @Override
    public void dispose () {
        // I dispose of things not managed by the asset manager...

        // Dispose of any other resources
        assetManager.dispose();
    }

/**
     * Convenience method to safely load textures. If the texture isn't found, a blank one is created and the error is logged.
     * @param imageName The name of the image that is being looked up.
     * @return
     */
    public TextureRegionDrawable getManagedTexture(String imageName) {
        try {
            return new TextureRegionDrawable(assetManager.get(ATLAS_NAME, TextureAtlas.class).findRegion(imageName));
        } catch(Exception e) {
            Gdx.app.error(getClass().getSimpleName(), "Couldn't get managed texture.", e);
            return getEmptyTexture();
        }
    }
    public TextureRegionDrawable getEmptyTexture() {
        return new TextureRegionDrawable(new TextureRegion(new Texture(new Pixmap(1,1, Pixmap.Format.RGBA8888))));
    }
}
诚然,我创建了一个时髦的方法来包装asset manager,这样我总是可以得到一个纹理(即使它是空白的),但是asset manager的正常使用如下所示:

Texture tex = assetManager.get("my/texture/name.png", Texture.class);

它也适用于其他类,如地图册和皮肤。

谢谢。你能给像我这样刚接触Libgdx的人举个具体的例子吗?谢谢。我想是的,所以基本上AssetManager非常适合管理大量图像文件。我会仔细研究的。谢谢。你能给像我这样刚接触Libgdx的人举个具体的例子吗?谢谢。我想是的,所以基本上AssetManager非常适合管理大量图像文件。我会进一步调查的。