简体   繁体   English

在LibGDX中处置Asset Manager

[英]Disposing Asset Manager in LibGDX

Here is my OOP setup: 这是我的OOP设置:

Class MainGame extends Game : MainGame类延伸游戏

... ...

public void create () {
    assets = new Assets(); // my custom Assets object (constructor creates manager and AssetDescriptors
    assets.load(); // loads Asset Descriptors into manager
    assets.getManager().finishLoading();
    batch = new SpriteBatch();
    setScreen(new PlayScreen(this, assets));
}

... ...

public void dispose () {

    assets.dispose(); // disposes asset manager
    super.dispose();
    batch.dispose();
}

Class PlayScreen implements Screen: PlayScreen类实现了Screen:

public PlayScreen(MainGame game, Assets assets) {

    this.assets = assets;
    this.game = game;

    background = assets.getManager().get(assets.getBackground());
    hero = new HeroSprite(290, 1100, this, assets);

    // Create Game camera and Hub also
}

public void render(float delta) {

    // Clear screen
    // Use game batch to draw to screen

}

public void dispose() {
    assets.dispose();
}

Class HeroSprite: HeroSprite类:

public HeroSprite(int x, int y, PlayScreen screen, Assets assets){

    this.assets = assets;
    this.screen = screen;

    // Animation Textures
    heroWalking = assets.getManager().get(assets.getWalking()); // This is an Asset Descriptor

    walkAnime = new Animation(heroWalking) // my own custom animation class which takes the spritesheet and returns the appropriate frames

    ...

 }

// This does not contain a dispose method

Sorr if there's a lot of code there I tried to minimize as much as possible. Sorr如果有很多代码,我尽量尽量减少代码。 My main question is if I am disposing correctly? 我的主要问题是我是否处理正确?

It seems ok, my game runs fine. 看来没问题,我的游戏运行良好。 But when I quit the game and re-enter a black screen shows. 但是当我退出游戏并重新进入黑屏显示时。 This happens both on Desktop and Android. 这在桌面和Android上都会发生。

A solution that works is to clear the memory on Android using Super Cleaner App. 一个有效的解决方案是使用Super Cleaner App清除Android上的内存。 Since it's a memory issue I figures it has something to do with me disposing incorrectly. 由于这是一个内存问题,我认为它与我处理错误有关。

EDIT : Revealing my Assets Class 编辑:揭示我的资产类

public class Assets {

    private AssetManager manager;

    private AssetDescriptor<Texture> background;
    private AssetDescriptor<Texture> wood;

    public AssetManager getManager() {
        return manager;
    }

    public AssetDescriptor<Texture> getBackground() {
        return background;
    }

    public AssetDescriptor<Texture> getWood() {
        return hero;
    }    

    public Assets(){

        manager = new AssetManager();

        background = new AssetDescriptor<Texture>("textures/static/bg.png", Texture.class);
        hero = new AssetDescriptor<Texture>("textures/static/hero.png", Texture.class);
        ...

    }

    public void load(){

        manager.load(background);
        manager.load(hero);
        ...

    }

    public void dispose(){
        manager.dispose();
    }
}

Screen.dispose() does not get called automatically , you should manually call dispose when you exit a screen and not needing the assets anymore. Screen.dispose() 不会自动调用,您应该在退出屏幕时手动调用dispose而不再需要资产。 But obviously when you need them again you need to load them again and you need a new AssetManager for that. 但显然当你再次需要它们时,你需要再次加载它们,你需要一个new AssetManager So if your assets won't take up that much you should just keep it in memory. 因此,如果您的资产不会占用那么多,您应该将其保留在内存中。

On exit however you need to dispose. 退出时,您需要处理。 Game.dispose() does get called automatically. Game.dispose()会自动调用。 Here you should call getScreen().dispose() and have everything needing disposing in the dispose() method of the effective Screen . 在这里你应该调用getScreen().dispose()并在有效Screendispose()方法中dispose()所有需要处理的内容。

Since currently it does not look like you are disposing between screens it might be because you have a static AssetManager, you should show your Assets() class. 由于目前它看起来不像你在屏幕之间处理,因为你有一个静态AssetManager,你应该显示你的Assets()类。

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

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