2

libgdx を使用しており、スプライトを描画したいタイル マップがあります。ただし、スプライトは実際のウィンドウに描画されるため、カメラを動かしてもスプライトは同じ場所にとどまります。スプライトをマップ上で動かしたい。

これは私が現在オブジェクトをレンダリングする方法です

    @Override
    public void render(float delta) {
        translateCamera();

        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        camera.update();

        renderer.setView(camera);

        renderer.render(bgLayers);
        batch.begin();

        batch.draw(splayerSprite, Gdx.graphics.getWidth() / 2,
                Gdx.graphics.getHeight() / 2);

        batch.end();
        renderer.render(fgLayers);

    }

常に画面の中央に表示されますが、たとえばカメラのように (W、A、S、D) で個別に移動し、方向キーでプレーヤーを移動できるようにしたいと考えています。次に、カメラをプレーヤーにロックしたい場合は、それ以外の場合は無料です。

私はlibgdxを初めて使用するので、ご容赦ください、ありがとう

4

2 に答える 2

2

batch.draw(splayerSprite, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2);

毎回画面の中央に描画するようにコードに指示しています。入力に基づいて変化する実際の値に変更する必要がありGdx.graphics.getWidth() / 2ます。Gdx.graphics.getHeight() / 2

編集#2batch.setProjectionmatrix(camera.combined);私が言及したすべてに加えて行が必要です。特定の行がすでにコードに含まれていることに気づきませんでした(デフォルトのlibGDXプロジェクトに含まれています)、その行でデモを実行しようとしませんでした削除されました。私が引き起こしたかもしれない混乱が解消されることを願っています。

編集:明らかに誰も私の答えを気に入らなかったので、クリーンなlibGDXゲームで指定されたコントロールを使用してデモを書きました。カメラがどこを向いているかに関係なく (カメラが変換されているため)、スプライトは常にグローバル画面の中央にレンダリングされていました。batch.draw()スプライトの位置を静的な位置の代わりに使用することが非常に必要です。そうしないと、スプライトは移動しません。

package com.me.mygdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;

public class MyGdxGame implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Texture texture;
    private Sprite sprite;
    private Sprite background;

    private boolean lockToSprite;
    private Vector2 vecCamera;
    private Vector2 vecSprite;

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

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

        lockToSprite = true;
        vecCamera = new Vector2();
        vecSprite = new Vector2();

        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.1f * sprite.getWidth(), 0.1f * sprite.getHeight());
        sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
        sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);

        background = new Sprite(region);
        background.setOrigin(background.getWidth() / 2, background.getHeight() / 2);
        System.out.println(background.getOriginX());
        background.setPosition(-background.getWidth() / 2, -background.getHeight() / 2);
    }

    @Override
    public void dispose() {
        batch.dispose();
        texture.dispose();
    }

    @Override
    public void render() {
        camera.translate(vecCamera);

        Gdx.gl.glClearColor(1, 1, 1, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        camera.update();

        camera.translate(vecCamera.cpy().mul(-1));

        float moveSensitivity = 0.9f;

        Vector2 vecInputSprite = new Vector2();
        if (Gdx.input.isKeyPressed(Keys.UP))
            vecInputSprite.y += moveSensitivity;
        if (Gdx.input.isKeyPressed(Keys.DOWN))
            vecInputSprite.y -= moveSensitivity;
        if (Gdx.input.isKeyPressed(Keys.LEFT))
            vecInputSprite.x -= moveSensitivity;
        if (Gdx.input.isKeyPressed(Keys.RIGHT))
            vecInputSprite.x += moveSensitivity;
        if (Gdx.input.isKeyPressed(Keys.N))
            vecSprite.set(new Vector2());

        Vector2 vecInputCamera = new Vector2();
        if (Gdx.input.isKeyPressed(Keys.W))
            vecInputCamera.y += moveSensitivity;
        if (Gdx.input.isKeyPressed(Keys.S))
            vecInputCamera.y -= moveSensitivity;
        if (Gdx.input.isKeyPressed(Keys.A))
            vecInputCamera.x -= moveSensitivity;
        if (Gdx.input.isKeyPressed(Keys.D))
            vecInputCamera.x += moveSensitivity;

        if (Gdx.input.isKeyPressed(Keys.R)) {
            vecCamera.set(new Vector2());
            lockToSprite = false;
        }

        if (vecInputCamera.len2() != 0)
            lockToSprite = false;
        else if (Gdx.input.isKeyPressed(Keys.L))
            lockToSprite = true;

        if (lockToSprite) {
            vecCamera.set(vecSprite);
        } else {
            vecCamera.add(vecInputCamera);
        }

        vecSprite.add(vecInputSprite);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        background.draw(batch);
        sprite.setPosition(vecSprite.x, vecSprite.y);
        sprite.draw(batch);
        //batch.draw(sprite, vecSprite.x, vecSprite.y);
        batch.end();
    }

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

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}
于 2013-08-04T04:35:38.253 に答える