3

LibGDX で 256px X 256px の画像を中央に配置しようとしています。私が使用しているコードを実行すると、ウィンドウの右上隅に画像がレンダリングされます。カメラの高さと幅については、 と を使用Gdx.graphics.getHeight();Gdx.graphcis.getWidth();ます。カメラの位置をカメラの幅を 2 ​​で割った値と高さを 2 で割った値に設定しました。これで画面の中央に配置されるはずですよね?テクスチャを描画するとき、その位置をカメラの幅と高さを 2 で割った値に設定します。画像が画面の中央に描画されないのはなぜですか、何か理解できないことがありますか?

ありがとう!

4

2 に答える 2

12

お使いのカメラは問題ないようです。テクスチャの位置を設定すると、そのテクスチャの左下隅の位置が設定されます。中心ではありません。したがって、画面の中心の座標に設定すると、その拡張はそのポイントの右側と上部のスペースをカバーします。中央に配置するには、テクスチャの幅の半分を x 座標から差し引き、テクスチャの高さの半分を y 座標から差し引く必要があります。これらの行に沿ったもの:

image.setPosition(Gdx.graphics.getWidth()/2 - image.getWidth()/2, 
Gdx.graphics.getHeight()/2 - image.getHeight()/2);
于 2012-09-17T13:23:41.980 に答える
6

カメラ位置でテクスチャを描画する必要があります-テクスチャの半分の寸法...

例えば:

class PartialGame extends Game {
    int w = 0;
    int h = 0;
    int tw = 0;
    int th = 0;
    OrthographicCamera camera = null;
    Texture texture = null;
    SpriteBatch batch = null;

    public void create() {
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getheight();
        camera = new OrthographicCamera(w, h);
        camera.position.set(w / 2, h / 2, 0); // Change the height --> h
        camera.update();
        texture = new Texture(Gdx.files.internal("data/texture.png"));
        tw = texture.getwidth();
        th = texture.getHeight();
        batch = new SpriteBatch();
    }

    public void render() {
        batch.begin();
        batch.draw(texture, camera.position.x - (tw / 2), camera.position.y - (th / 2));
        batch.end();
    }
}
于 2012-09-17T13:22:44.457 に答える