在我的游戏中,我目前有两个屏幕。 MenuScreen
和GameScreen
。通过菜单中的播放选项,播放器可以切换到GameScreen并启动游戏,然后通过Escape他可以返回到MenuScreen。当我切换到hide()
方法中的另一个屏幕并在屏幕I切换到的构造函数中为新屏幕加载所需的资产时,我处理使用过的资产。问题是当我切换回时,不会渲染/播放纹理和声音效果。
例如,当我在MenuScreen
中开始游戏时,切换到GameScreen
一切都很好。但当我切换回MenuScreen
时,MenuScreen
只是一个黑色的窗口。当我再次切换到GameScreen
时,除了BitmapFont之外它也是黑色的。
也许我处理这个问题的方式存在一个根本性的缺陷。我试图从我在这里发布的代码中遗漏尽可能多的不必要的东西,但我担心它仍然太多了。
RessourceLoader类:
public class RessourceLoader {
public static AssetManager manager;
public static void create() {
manager = new AssetManager();
}
public static void loadMenuScreen() {
manager.load("gfx/menuBackground.png", Texture.class);
}
public static void getMenuScreen() {
menuBackground = manager.get("gfx/menuBackground.png", Texture.class);
}
public static void disposeMenuScreen() {
menuBackground.dispose();
}
public static void loadGameScreen() {
// load GameScreen Assets through AssetManager
}
public static void getGameScreen() {
// get GameScreen Assets through AssetManager
}
public static void disposeGameScreen() {
// dispose all GameScreen Assets
}
public static void dispose() {
manager.dispose();
}
}
MenuScreen类:
public class MenuScreen implements Screen {
// Game starts in the MenuScreen
// Instance of game
private PHGame game;
// Orthographic camera
private OrthographicCamera cam;
public MenuScreen(PHGame phGame) {
game = phGame;
RessourceLoader.loadMenuScreen();
RessourceLoader.manager.finishLoading();
RessourceLoader.getMenuScreen();
cam = new OrthographicCamera();
cam.setToOrtho(true, 640, 480);
game.batcher.setProjectionMatrix(cam.combined);
}
@Override
public void render(float delta) {
// Fills background with black to avoid flickering
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Begin Drawing
game.batcher.begin();
// Draw Menu
// Stop drawing
game.batcher.end();
// Pressing Space confirms currently selected menu item
if (GameKeys.isPressed(GameKeys.SPACE)) {
game.setScreen(new GameScreen(game));
}
// Update Key Presses
GameKeys.update();
}
@Override
public void hide() {
dispose();
}
@Override
public void dispose() {
RessourceLoader.disposeMenuScreen();
}
}
GameScreen类:
public class GameScreen implements Screen {
// Instance of game
private PHGame game;
private GameWorld world;
private GameRenderer renderer;
private float runTime;
public GameScreen(PHGame phGame) {
game = phGame;
RessourceLoader.loadGameScreen();
RessourceLoader.manager.finishLoading();
RessourceLoader.getGameScreen();
world = new GameWorld(game, this);
renderer = new GameRenderer(world, game);
}
@Override
public void render(float delta) {
// runTime is the amount of time the game is running
runTime += delta;
// Updates the Game World
world.update(delta);
// Renders everything
renderer.render(runTime);
// Update Key Presses
GameKeys.update();
}
@Override
public void hide() {
dispose();
}
@Override
public void dispose() {
RessourceLoader.disposeGameScreen();
}
}
GameRenderer类:
public class GameRenderer {
// Instance of PHGame
PHGame game;
// Instance of Game World
private GameWorld world;
// Orthographic Camera
private OrthographicCamera cam;
// If true hitbox's will be shown
private boolean showHitbox;
// Game Objects
private Player player;
public GameRenderer(GameWorld world, PHGame game) {
this.game = game;
this.world = world;
player = world.getPlayer();
cam = new OrthographicCamera();
cam.setToOrtho(true, 640, 480);
showHitbox = false;
game.batcher.setProjectionMatrix(cam.combined);
}
public void render(float runTime) {
// draw objects and hud
}
}
如果对我的问题有任何疑问,我会尽力回答。
答案 0 :(得分:2)
请参阅github文章“管理您的资产”。 AssetManagers不应该是静态的。 “这通常会导致黑色/缺失纹理或不正确的资产。”
在您处置资产管理器之后,无法使用它。而是使用manager.unload来卸载资产。 manager.unload("gfx/menuBackground.png");
编辑:
我也没有看到任何重写的show()方法。如果您想要恢复资产,则需要每次都在屏幕的show方法中加载资源。