私はLibGDXでゲームを作成しています。私はPixel Dungeonで使われているタイルを取り出し、Tiledを使ってタイルマップを作成しました。タイルマップ上にスプライトを表示するlibgdx
主人公のクラスがActor
のサブクラスであり、かつ文字がアニメーション化されているので、私はスプライトを描画するために、このコードを使用しています:私は、Eclipseでゲームを起動すると
if (direction.isEastwards() || direction.isNorthwards()) {
Vector3 projectedPosition = getLocation().getCamera().project(
new Vector3(16 * getX() + 1, Gdx.graphics.getHeight()
- (16 * getY()) - 15, 0));
batch.draw(current.getKeyFrame(
animTime += Gdx.graphics.getDeltaTime(), true),
projectedPosition.x, projectedPosition.y);
} else {
Vector3 projectedPosition = getLocation().getCamera().project(
new Vector3(16 * getX() + 13, Gdx.graphics.getHeight()
- (16 * getY()) - 15, 0));
batch.draw(current.getKeyFrame(
animTime += Gdx.graphics.getDeltaTime(), true),
projectedPosition.x, projectedPosition.y);
}
、スプライトが最初に表示されます正しい場所に保管してください。しかし、画面のサイズを変更すると、スプライトは正しい位置になくなり、最終的にはマップから消えてしまいます。
投影を使用する前に同じことが起こりました。問題は投影に関連していると考えました。これは私が一時間後にこれを解決して助けを求める決心をする前に私が調べたことのない地域です。
文字がどの方向を向いているかによってアニメーションが反転します。そのため、if/else節が存在します。
補足:ステージはnew Stage(new ExtendViewport(800,600),batch);
で作成され、resizeメソッドはカメラを更新し、バッチは投影行列に設定されます。ここで
は、より多くの関連するコードです:
カメラとマップの初期化:
camera.update();
batch.setProjectionMatrix(camera.combined);
mapRenderer.setView(camera);
mapRenderer.render();
stage.act();
stage.draw();
リサイズ方法:
camera.viewportWidth=width;
camera.viewportHeight=height;
camera.update();
俳優ドロー法:
camera=new OrthographicCamera();
camera.setToOrtho(false,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
mapRenderer=new OrthogonalTiledMapRenderer(map,batch);
Renderメソッドを
@Override
public void draw(Batch batch, float parentAlpha) {
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
if (direction.isEastwards() || direction.isNorthwards()) {
Vector3 projectedPosition = getLocation().getCamera().project(
new Vector3(16 * getX() + 1, Gdx.graphics.getHeight()
- (16 * getY()) - 15, 0));
batch.draw(current.getKeyFrame(
animTime += Gdx.graphics.getDeltaTime(), true),
projectedPosition.x, projectedPosition.y);
} else {
Vector3 projectedPosition = getLocation().getCamera().project(
new Vector3(16 * getX() + 13, Gdx.graphics.getHeight()
- (16 * getY()) - 15, 0));
batch.draw(current.getKeyFrame(
animTime += Gdx.graphics.getDeltaTime(), true),
projectedPosition.x, projectedPosition.y);
}
// Walk animation displays for 0.2 seconds
if (current == walk && animTime >= 0.2) {
current = idle;
animTime = 0;
}
}
私は間違いなく 'batch.setProjectionMatrix(camera.combined);'を追加しました。私は更新行を忘れたかどうかを確認します。 – pixelherodev
私の質問が更新されました。関連するコードをさらに追加しました。 – pixelherodev
また、サイズ変更でビューポートの更新を適用しますか?次のようなものです:stage.getViewport()。update(Gdx.graphics.getWidth()、Gdx.graphics.getHeight、true);またはあなたが画面サイズのために必要なものは何ですか? – Wouf