libGDX ViewPort Android设备 [英] libGDX ViewPort Android devices

查看:95
本文介绍了libGDX ViewPort Android设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用fitviewport或viewport?我需要将屏幕适合许多android设备.我不知道如何使用它,谢谢您的帮助,如果您可以为我粘贴一些代码,我非常感谢...

How can i use the fitviewport or viewport? i need to fit the screen to many android devices as posible. I dont have idea how to use that, thank you for your help, if you can paste some code for me i aprecciate that...

public void show(){
    batch = main.getBatch();
    texture = new Texture(Gdx.files.internal("spacebg.png"));
    Texture texturaBola = new Texture(Gdx.files.internal("bola.png"));
    ball = new Ball(Gdx.graphics.getWidth() / 2 - texturaBola.getWidth() / 2, Gdx.graphics.getHeight() / 2 - texturaBola.getHeight() / 2);
    Texture texturaPala= new Texture(Gdx.files.internal("pala.png"));
    Lpaddle = new LeftPaddle(80, Gdx.graphics.getHeight()/2 -texturaPala.getHeight() /2);
    Rpaddle = new RightPaddle(Gdx.graphics.getWidth() -100, Gdx.graphics.getHeight()/2 - texturaPala.getHeight() /2, ball);
    font = new BitmapFont();
    font.setColor(Color.WHITE);
    font.setScale(1f);
    puntuacion = 0;

 }
public void render(float delta){
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    updatePuntuacion();
    Lpaddle.update();
    Rpaddle.update();
    ball.update(Lpaddle, Rpaddle);
    batch.begin();
    batch.draw(texture, 0, 0,texture.getWidth(), texture.getHeight());
    ball.draw(batch);
    Lpaddle.draw(batch);
    Rpaddle.draw(batch);
    font.draw(batch, "Points: " + Integer.toString(puntuacion), Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5);
    font.draw(batch, "High score: " + Integer.toString(puntuacionMaxima),Gdx.graphics.getWidth() - Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5);
    batch.end();
}

private void updatePuntuacion(){
    if(ball.getBordes().overlaps(Lpaddle.getBordes())) { 
        puntuacion = puntuacion + 1;
        if(puntuacion > puntuacionMaxima)
        puntuacionMaxima = puntuacion;

    }
    if(ball.getBordes().x <= 0) 
        sonidoex.play();

    if(ball.getBordes().x <= 0)
    puntuacion =0;

    if(ball.getBordes().x <=0)
        Gdx.input.vibrate(1000);

    if(ball.getBordes().x <=0)
        Screens.juego.setScreen(Screens.MAINSCREEN);

    ball.comprobarPosicionBola();
    }
public void hide(){
    font.dispose();
    texture.dispose();
}

@Override
public void dispose(){
    preferencias.putInteger("puntuacionMaxima", puntuacionMaxima);
    preferencias.flush();
}

public void resize(int width, int height){      
    float widthImage = texture.getWidth();
    float heightImage = texture.getHeight();
    float r = heightImage / widthImage;
    if(heightImage > height) { 
        heightImage = height;
        widthImage = heightImage / r;
    }
    if(widthImage > width) { 
        widthImage = width;
        heightImage = widthImage * r;
    }
    escala = width / widthImage; 

}

推荐答案

某些代码

https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/ViewportTest1.java

https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/ViewportTest2.java

https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/ViewportTest3.java

和文档 https://github.com/libgdx/libgdx/wiki/Viewports

运行测试,您将了解其工作方式.

Run the tests and you will understand the way it works.

这篇关于libGDX ViewPort Android设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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