gpt4 book ai didi

android - 需要关于 Libgdx Scaling 的建议

转载 作者:行者123 更新时间:2023-11-30 03:19:51 26 4
gpt4 key购买 nike

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

Link to image

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

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");
}

最佳答案

您需要设置相机和舞台。

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

OrthographicCamera camera;

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

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

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

并在渲染方法中更新相机

camera.update();

对我来说很好..

关于android - 需要关于 Libgdx Scaling 的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19352514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com