Java Android的视口问题
当我在桌面上运行游戏时,效果很好,但当我在android设备上运行游戏时,图像看起来被切成了一半,当我使用“播放”按钮时,游戏关闭,有人能帮我吗?多谢各位Java Android的视口问题,java,android,eclipse,libgdx,Java,Android,Eclipse,Libgdx,当我在桌面上运行游戏时,效果很好,但当我在android设备上运行游戏时,图像看起来被切成了一半,当我使用“播放”按钮时,游戏关闭,有人能帮我吗?多谢各位 public class GameScreen extends AbstractScreen { private Viewport viewport; private Camera camera; private SpriteBatch batch; private Texture texture;
public class GameScreen extends AbstractScreen {
private Viewport viewport;
private Camera camera;
private SpriteBatch batch;
private Texture texture;
private float escala;
private Paddle Lpaddle, Rpaddle;
private Ball ball;
private BitmapFont font;
private int puntuacion, puntuacionMaxima;
private Preferences preferencias;
private Music music;
private Sound sonidoex;
public GameScreen(Main main) {
super(main);
preferencias = Gdx.app.getPreferences("PuntuacionAppPoints");
puntuacionMaxima = preferencias.getInteger("puntuacionMaxima");
music =Gdx.audio.newMusic(Gdx.files.internal("bgmusic.mp3"));
music.play();
music.setVolume((float) 0.3);
music.setLooping(true);
sonidoex = Gdx.audio.newSound(Gdx.files.internal("explosion5.wav"));
}
public void create(){
camera = new PerspectiveCamera();
viewport = new FitViewport(800, 480, camera);
}
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;
if(Gdx.app.getType()== ApplicationType.Android)
viewport.update(width, height);
}
}
公共类游戏屏幕扩展了AbstractScreen{
私有视口;
私人摄像机;
专用SpriteBatch批次;
私有纹理;
私人浮动埃斯卡拉;
私人桨Lpaddle,Rpaddle;
私人舞会;
专用位图字体;
puntuacion私人酒店,puntuacionMaxima;
私人偏好;
私人音乐;
私人音响;
公共游戏屏幕(主屏幕){
超级(主);
preferencias=Gdx.app.getPreferences(“puntuacionappoints”);
puntuacionMaxima=preferencias.getInteger(“puntuacionMaxima”);
music=Gdx.audio.newMusic(Gdx.files.internal(“bgmusic.mp3”);
音乐。播放();
音乐。设置音量((浮动)0.3);
音乐。设置循环(真);
sonidoex=Gdx.audio.newSound(Gdx.files.internal(“explosion5.wav”);
}
公共void create(){
摄像头=新透视摄像头();
视口=新视口(800480,摄像头);
}
公开展览({
batch=main.getBatch();
纹理=新纹理(Gdx.files.internal(“spacebg.png”);
Texture TextureAbola=新纹理(Gdx.files.internal(“bola.png”);
ball=新球(Gdx.graphics.getWidth()/2-TextureAbola.getWidth()/2,Gdx.graphics.getHeight()/2-TextureAbola.getHeight()/2);
Texture TextureApala=新纹理(Gdx.files.internal(“pala.png”);
Lpaddle=newleftpable(80,Gdx.graphics.getHeight()/2-texturaPala.getHeight()/2);
Rpaddle=新的右桨(Gdx.graphics.getWidth()-100,Gdx.graphics.getHeight()/2-texturaPala.getHeight()/2,ball);
font=新的位图字体();
font.setColor(Color.WHITE);
字体设置比例(1f);
puntuacion=0;
}
公共无效渲染(浮动增量){
glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
updatePantuacion();
Lpaddle.update();
Rpaddle.update();
更新(Lpaddle,Rpaddle);
batch.begin();
batch.draw(纹理,0,0,texture.getWidth(),texture.getHeight());
球.抽(批);
Lpaddle.draw(批处理);
Rpaddle.draw(批量);
font.draw(批处理,“点:”+Integer.toString(puntuacion),Gdx.graphics.getWidth()/4,Gdx.graphics.getHeight()-5);
font.draw(批处理,“高分:”+Integer.toString(puntuacionMaxima),Gdx.graphics.getWidth()-Gdx.graphics.getWidth()/4,Gdx.graphics.getHeight()-5);
batch.end();
}
私有void updatePantuacion(){
if(ball.getBordes().overlaps(Lpaddle.getBordes()){
puntuacion=puntuacion+1;
if(puntuacion>puntuacionMaxima)
puntuacionMaxima=puntuacion;
}
如果(ball.getBordes().x首先,使用正交照相机
camera=new OrthographicCamera(800,480);
camera.position.set(800/2f,480/2f,0);
viewport=new FitViewport(800,480,camera);
现在0,0位于屏幕的左下角
在开始批处理之前,不要忘记设置投影矩阵
batch.setProjectionMatrix(camera.combined);
batch.begin();
////
////
batch.end();