在Libgdx使用屏幕时重用code [英] Reuse code when using screens in Libgdx

查看:257
本文介绍了在Libgdx使用屏幕时重用code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从阅读其他人就如何使不同的屏幕code时,我的理解。你做一个主处理类之类的...然后为每个屏幕上一个新的类。

From what I understand when reading other peoples code on how to make different screens. You do a main handler class sort of... And then create a new class for each screen.

这是混淆了我的事情是,当你创建一个新的屏幕,你必须重新定义一切,那将被渲染,就像SpriteBatch,精灵,字体等有什么办法重用这类事情在所有屏幕?我的意思是,如果我有10个屏幕,并希望能够利用每个屏幕上的文字。难道真的良好的编程习惯作出新的BitmapFont所有10个屏幕的类?

The thing that confuses me is that whenever you create a new screen, you have to redefine everything that's going to be rendered, like SpriteBatch, sprites, fonts, etc. Is there any way to reuse these kind of things in all screens? I mean, if I have 10 screens, and want to be able to draw text on every screen. Is it really good programming practice to make a new BitmapFont for all 10 screen classes?

推荐答案

我已经创建了一个包含所有屏幕的共同对象的抽象Screen类,我的屏幕中的每一个扩展这个抽象类。这看起来像这样:

I've created an Abstract Screen class that contains all the common objects for a screen, every one of my screens extend this abstract class. And that looks like this:

public abstract class AbstractScreen implements Screen {
    protected final Game game;

    protected InputMultiplexer multiInputProcessor;
    protected ScreenInputHandler screenInputHandler;

    protected Stage uiStage;
    protected Skin uiSkin;

    public AbstractScreen(Game game) {
        this.game = game;
        this.uiStage = new Stage();
        this.uiSkin = new Skin();

        this.screenInputHandler = new ScreenInputHandler(game);
        this.multiInputProcessor = new InputMultiplexer();

        multiInputProcessor.addProcessor(uiStage);
        multiInputProcessor.addProcessor(screenInputHandler);

        Gdx.input.setInputProcessor(multiInputProcessor);
    }

    private static NinePatch processNinePatchFile(String fname) {
        final Texture t = new Texture(Gdx.files.internal(fname));
        final int width = t.getWidth() - 2;
        final int height = t.getHeight() - 2;
        return new NinePatch(new TextureRegion(t, 1, 1, width, height), 3, 3, 3, 3);
    }

    @Override
    public void render (float delta) {
        Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        uiStage.act(Math.min(Gdx.graphics.getDeltaTime(), 1 / 30f));
        uiStage.draw();
        Table.drawDebug(uiStage);
    }

    @Override
    public void resize (int width, int height) {
    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {
        dispose();
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        uiStage.dispose();
        uiSkin.dispose();
    }
}

当我想创建一个新的类我只是继承抽象的屏幕,并添加我需要什么。比如我有一个基本的信用屏幕,我只需要创建的组件,但抽象的画面吸引它:

When I want to create a new class I just extend the abstract screen and add what I need. For example I have a basic credits screen, I just need to create the components but the abstract screen draws it:

public class CreditsScreen extends AbstractScreen {

    public CreditsScreen(final Game game) {
        super(game);

        // Generate a 1x1 white texture and store it in the skin named "white".
        Pixmap pixmap = new Pixmap(1, 1, Format.RGBA8888);
        pixmap.setColor(Color.WHITE);
        pixmap.fill();
        uiSkin.add("white", new Texture(pixmap));

        // Store the default libgdx font under the name "default".
        BitmapFont buttonFont = new BitmapFont();
        buttonFont.scale(scale);
        uiSkin.add("default", buttonFont);

        // Configure a TextButtonStyle and name it "default". Skin resources are stored by type, so this doesn't overwrite the font.
        TextButtonStyle textButtonStyle = new TextButtonStyle();
        textButtonStyle.up = uiSkin.newDrawable("white", Color.DARK_GRAY);
        textButtonStyle.down = uiSkin.newDrawable("white", Color.DARK_GRAY);
        textButtonStyle.checked = uiSkin.newDrawable("white", Color.BLUE);
        textButtonStyle.over = uiSkin.newDrawable("white", Color.LIGHT_GRAY);
        textButtonStyle.font = uiSkin.getFont("default");
        uiSkin.add("default", textButtonStyle);

        // Create a table that fills the screen. Everything else will go inside this table.
        Table table = new Table();
        table.setFillParent(true);
        uiStage.addActor(table);

        table.debug(); // turn on all debug lines (table, cell, and widget)
        table.debugTable(); // turn on only table lines

        // Label
        BitmapFont labelFont = new BitmapFont();
        labelFont.scale(scale);
        LabelStyle labelStyle = new LabelStyle(labelFont, Color.BLUE);
        uiSkin.add("presents", labelStyle);
        final Label myName = new Label("Credits and all that stuff", uiSkin, "presents");

        table.add(myName).expand().center();
    }
}

我也有一个处理所有屏幕输入一个类,这样做的具体目的是处理如何围绕不同的屏幕后退按钮工程。而在抽象类中创建此输入处理程序类。

I also have a single class that handles the input for all the screens, the specific purpose for this is to handle how the back button works around the different screens. And this input handler class is created in the abstract class.

public class ScreenInputHandler implements InputProcessor {

    private final Game game;

    public ScreenInputHandler(Game game) {
        this.game = game;
    }

    @Override
    public boolean keyDown(int keycode) {
        if(keycode == Keys.BACK || keycode == Keys.BACKSPACE){       
            if (game.getScreen() instanceof MainMenuScreen) {
                Gdx.app.exit();
            }
            if (game.getScreen() instanceof GameScreen) {
                World.getInstance().togglePause(false);
            }
            if (game.getScreen() instanceof CreditsScreen) {
                game.setScreen(new MainMenuScreen(game));
            }
        }
        return false;
    }

}

这篇关于在Libgdx使用屏幕时重用code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆