这是我的OOP设置:
Class 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实现了屏幕:
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:
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如果那里有很多代码我试图尽可能地减少代码。我的主要问题是我是否处理正确?
看来没问题,我的游戏运行良好。但是当我退出游戏并重新进入黑屏显示时。这在桌面和Android上都会发生。
有效的解决方案是使用Super Cleaner App清除Android上的内存。由于它是一个记忆问题,我认为它与我处理错误有关。
编辑:揭示我的资产类
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();
}
}
答案 0 :(得分:0)
Screen.dispose()
does not get called automatically,当您退出屏幕并且不再需要资产时,您应该手动调用dispose。但显然当你再次需要它们时,你需要再次加载它们,你需要new AssetManager
。因此,如果您的资产不会占用太多,您应该将其保留在内存中。
退出时,您需要处理。 Game.dispose()
会自动调用。在这里,您应该致电getScreen().dispose()
,并在有效dispose()
的{{1}}方法中处理所有需要处理的内容。
由于目前看起来你在屏幕之间处理它可能是因为你有一个静态的AssetManager,你应该显示你的Screen
类。