2

デスクトップでより高い解像度で実行したいlibgdxを使用してゲームを作成していますが、Androidでより低い解像度で実行すると、すべてが正しく縮小されます。これを行う最善の方法は、ピクセル パーフェクト カメラを使用せず、代わりにワールド座標を使用することだと読みましたが、それを正しく行う方法がわかりません。

これは私が今持っているコードです:

@Override
public void create() {
    characterTexture = new Texture(Gdx.files.internal("character.png"));
    characterTextureRegion = new TextureRegion(characterTexture, 0, 0,  100, 150);
    batch = new SpriteBatch();


    Gdx.gl10.glClearColor(0.4f, 0.6f, 0.9f, 1);

    float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
    camera= new OrthographicCamera(aspectRatio, 1.0f);

}

@Override
public void render() {
    GL10 gl = Gdx.graphics.getGL10();

    gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    camera.update();
    camera.apply(gl);
    batch.setProjectionMatrix(camera.combined);

    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    draw();
}

private void draw() {
    //batch.getProjectionMatrix().set(camera.combined);
    batch.begin();

    batch.draw(characterTextureRegion, 0, 0, // the bottom left corner of the box, unrotated
            1f, 1f, // the rotation center relative to the bottom left corner of the box
            0.390625f, 0.5859375f, // the width and height of the box
            1, 1, // the scale on the x- and y-axis
            0); // the rotation angle


    batch.end();
}

私が使用しているテクスチャは 256x256 で、実際の画像は 100x150 です。

これは、ゲームを実行したときに得られる結果です: http://i.imgur.com/HV9Bi.png

これが元の画像であることを考えると、レンダリングされるスプライトは巨大です: http://i.imgur.com/q1cZT.png

スプライトが元のサイズでレンダリングされ、異なる解像度でプレイしたときにゲームのスケールを正しく維持できるようにするための最善の方法は何ですか?

私は2つの解決策しか見つけられませんでしたが、どちらも好きではありません。

  1. カメラにピクセル座標を使用した場合、画像はどのように表示されるかを示していましたが、別の解像度で携帯電話に配置すると、まったくスケーリングされませんでした.
  2. 描画時にテクスチャ領域を縮小できますが、拡大縮小する正しい数を見つけようとするのは非常に面倒なので、より良い方法があるようです。
4

2 に答える 2

4

Libgdxセットアップツールを使用したことがありますか?それを使用してプロジェクトを作成すると、サンプル画像が表示されます。画面をどのサイズに変更しても、比率は正しく保たれているようです。

public class RotationTest implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
Stage stage;
public boolean leonAiming = true;

@Override
public void create() {      
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camera = new OrthographicCamera(1, h/w);
    batch = new SpriteBatch();

    texture = new Texture(Gdx.files.internal("data/libgdx.png"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);

    sprite = new Sprite(region);
    sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
    sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
    sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);   }....

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

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    sprite.draw(batch);
    batch.end();
于 2013-01-31T16:26:36.043 に答える
0

まず第一に、世界への境界を修正する必要があります (私はあなたのゲームを意味します)。その世界では、あなたのアクター(ゲームキャラクター)だけがプレイする必要があります。境界をまたいでいる場合は、カメラで上下左右を表示するように管理します。

于 2011-10-24T10:12:46.137 に答える