libGDX ViewPort Android设备

问题描述:

如何使用fitviewport或视口?我需要适合屏幕许多Android设备作为posible。我没有想法如何使用,谢谢你的帮助,如果你能贴给我一些代码,我aprecciate那......libGDX ViewPort Android设备

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; 

} 
+0

您是否试过阅读文档? http://developer.android.com/guide/webapps/targeting.html – dave 2014-10-03 12:24:11

+1

该wiki应该帮助:https://github.com/libgdx/libgdx/wiki/Viewports – Springrbua 2014-10-03 14:02:24

+0

我不明白:/你可以更具体请? – JAnoob92 2014-10-05 14:32:46

+0

你究竟想让我变得更具体?我向你提供了视口的文档,这就是你需要做的一切。复制粘贴示例中的重要代码,即可完成。 – 2014-10-05 16:50:10