Java - Libgdx:保持演员的位置,甚至屏幕的分辨率变化

时间:2016-09-27 07:03:52

标签: libgdx screen resolution actor responsive

问题是下一个:我的游戏是为1080x1920像素的屏幕构建的,但是如果我在其他分辨率下运行它我的演员'职位正在转变。

有些图片,如果您还没有理解:

〜> original resolution

〜> other resolution

我在Google,Stackoverflow,论坛上寻找了几天的答案,无法找到问题的正确答案,这是我的代码:

public class PlayScreen implements Screen {

    private Main game;

    private Stage stage;

    Music music;
    Sound sound;




    private class MenuGame extends Actor {
        Texture menu = new Texture(Gdx.files.internal("sprite/menu.png"));

        @Override
        public void draw(Batch batch, float alpha) {
            batch.draw(menu, 0, 0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        }
    }

    private class NewGame extends Actor {
        Texture newgame = new Texture(Gdx.files.internal("sprite/new.png"));

        @Override
        public void draw(Batch batch, float alpha) {
            batch.draw(newgame, 350, 1000);
        }
    }

    private class LoadGame extends Actor {
        Texture loadgame = new Texture(Gdx.files.internal("sprite/load.png"));

        @Override
        public void draw(Batch batch, float alpha) {
            batch.draw(loadgame, 350, 830);
        }
    }

    private class CreditsGame extends Actor {
        Texture creditsgame = new Texture(Gdx.files.internal("sprite/credits.png"));

        @Override
        public void draw(Batch batch, float alpha) {
            batch.draw(creditsgame, 350, 660);
        }
    }

    private class DonsGame extends Actor {
        Texture donsgame = new Texture(Gdx.files.internal("sprite/dons.png"));

        @Override
        public void draw(Batch batch, float alpha) {
            batch.draw(donsgame, 350, 490);
        }
    }


    // our create()

    public PlayScreen(final Main game) {
        this.game = game;


        Gdx.input.setInputProcessor(stage);

        final Sound sound = Gdx.audio.newSound(Gdx.files.internal("sound/buttonsound.mp3"));

        music = Gdx.audio.newMusic(Gdx.files.internal("sound/brun.mp3"));
        music.setLooping(true);
        music.setVolume(0.2f);
        music.play();




        // actors

        MenuGame menu = new MenuGame();     // BACKGROUND
        stage.addActor(menu);

        final NewGame newGame = new NewGame();   // BOUTON NEW
        stage.addActor(newGame);
        newGame.setX(350);
        newGame.setY(1000);
        newGame.setWidth(410);
        newGame.setHeight(100);


        LoadGame loadGame = new LoadGame();   // BOUTON LOAD
        stage.addActor(loadGame);
        loadGame.setX(350);
        loadGame.setY(830);
        loadGame.setWidth(410);
        loadGame.setHeight(100);

        CreditsGame creditsGame = new CreditsGame();   // BOUTON CREDITS
        stage.addActor(creditsGame);
        creditsGame.setX(350);
        creditsGame.setY(660);
        creditsGame.setWidth(410);
        creditsGame.setHeight(100);

        DonsGame donsGame = new DonsGame();   // BOUTON DONS
        stage.addActor(donsGame);
        donsGame.setX(350);
        donsGame.setY(490);
        donsGame.setWidth(410);
        donsGame.setHeight(100);






        // actions

        newGame.addListener(new ClickListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)   // NEW BOUTON ACTION
            {
                sound.play(0.2f);
                music.stop();
                game.setScreen(new Story(game));
                return true;
            }
        });


        loadGame.addListener(new ClickListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)   // LOAD BOUTON ACTION
            {
                sound.play(0.2f); //faire un truc ici
                return true;
            }

        });




        creditsGame.addListener(new ClickListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)   // CREDITS BOUTON ACTION
            {
                sound.play(0.2f);//faire un truc ici
                return true;
            }
        });


        donsGame.addListener(new ClickListener() {
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)   // DONS BOUTON ACTION
            {
                sound.play(0.2f);//faire un truc ici
                return true;
            }
        });


    }



    @Override
    public void show() {
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.draw();

    }

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

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {
        stage.dispose();
        music.dispose();
        sound.dispose();
    }

}

2 个答案:

答案 0 :(得分:1)

您在硬编码值之后定位所有内容。所以现在你的游戏非常依赖分辨率。

您需要使用的是视口。 以下是一些可以帮助您入门的链接:

Brent Aureli - Aspect Ratios & Viewports

libgdx github - viewports

libgdx github - scene2d

答案 1 :(得分:0)

这种情况正在发生,因为不同的设备具有不同的屏幕尺寸。所以它会有所不同。为了避免这种情况,你必须使用viewPort。为了更清楚你必须阅读关于多分辨率和viewPort的libgdx api。在这里,我会建议你一些示例代码,以避免这种多屏幕问题。请按照这种模式解决您的问题。

// in your main class
public class myGame extends ApplicationAdapter {

    public OrthographicCamera camera
    public Viewport viewPort
    private SpriteBatch batch;
    private BitMapFont myScoreFont
    public myGame() {

    }

    @Override
    public void create() {
            myScoreFont = new BitmapFont(Gdx.files.internal(font.txt), true);
        batch = new SpriteBatch();
        float w = Gdx.graphics.getWidth();
        float h = Gdx.graphics.getHeight();

        camera = new OrthographicCamera();
        camera.position.set(0, 0, 0);
        camera.update();
        camera.setToOrtho(false, Constants.APP_WIDTH, Constants.APP_HEIGHT);
        viewPort = new FillViewport(1280, 800, camera);


    }

    @Override
    public void dispose() {
        batch.dispose();
    }



    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
        float deltaTime = Gdx.graphics.getDeltaTime();
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        myScoreFont.draw(batch,"myText",0,0)
        batch.end();

    }

    @Override
    public void resize(int width, int height) {
        viewPort.update(width, height);
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

有关详细信息,您必须阅读以下关于视口的链接

https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/viewport/FillViewport.html