1

这可能是一个在某处解决的简单问题,但我找不到。我确实希望有人能带领我走上正确的道路。我的应用程序的设计分辨率是 800x480。为了在更高分辨率的设备上保持正确的纵横比,我关注了这篇文章,并设法在更大的屏幕(nexus 7)的两侧获得了“黑条”(我使用蓝色进行测试)。但是,似乎舞台没有按比例缩放以覆盖屏幕。请看下面的截图,蓝色是 Gdx.gl.glClearColor(0.5f, 1, 1, 1); 黑色矩形 (800x480) 是实际的 Sprite。

链接到图片

我不确定我哪里出错了。任何帮助深表感谢。下面的代码:

private SpriteBatch batch;
private Texture splashTexture;
private Sprite splashSp;
TextureRegion splashTr;
Stage stage;

@Override
public void create() {

    stage = new Stage();

    batch = new SpriteBatch();

    splashTexture = new Texture(Gdx.files.internal("img/splashTexture.png"));
    splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    TextureRegion splashTr = new TextureRegion(splashTexture, 0, 0, 800, 480);

    splashSp = new Sprite(splashTr);

    Gdx.app.log("myapp", "Creating game");
}

@Override
public void render() {      
    Gdx.gl.glClearColor(0.5f, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    stage.draw();

    batch.begin();
    batch.draw(splashSp, 0, 0);

    batch.end();
}

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

    Gdx.app.log("myapp", "width:" + width + " height:" + height);
    Vector2 size = Scaling.fit.apply(800, 480, width, height);
    int viewportX = (int)(width - size.x) / 2;
    int viewportY = (int)(height - size.y) / 2;
    int viewportWidth = (int)size.x;
    int viewportHeight = (int)size.y;
    Gdx.app.log("myapp", "viewportWidth:" + viewportWidth + " viewportHeight:" + viewportHeight);
    Gdx.app.log("myapp", "viewportX:" + viewportX + " viewportY:" + viewportY);
    Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
    stage.setViewport(800, 480, true);

    Gdx.app.log("myapp", "Resizing game");
}
4

1 回答 1

1

您需要设置相机和舞台。

首先像这样声明一个相机变量

OrthographicCamera camera;

然后在创建方法中执行此操作

camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
camera.update();

mystage = new Stage(800, 480, false);

并在渲染方法中更新相机

camera.update();

我工作得很好..

于 2013-10-14T06:23:41.330 回答