3

libgdx ゲームにタッチ スクロールを実装しようとしています。部屋のパノラマであるワイド画像があります。ユーザーが部屋の周りを見ることができるように、画像をスクロールできるようにしたい。一定の距離をスクロールできるようにしていますが、新しい touchDragged イベントが登録されると、画像は元の位置に戻ります。

これが私がそれを実装している方法です

public class AttackGame implements ApplicationListener {

AttackInputProcessor inputProcessor;
Texture backgroundTexture; 
TextureRegion region;
OrthographicCamera cam;
SpriteBatch batch;
float width;
float height;
float posX;
float posY;

@Override
public void create() {
    posX = 0;
    posY = 0;
    width = Gdx.graphics.getWidth();
    height = Gdx.graphics.getHeight();  
    backgroundTexture = new Texture("data/pancellar.jpg");
    region = new TextureRegion(backgroundTexture, 0, 0, width, height);
    batch = new SpriteBatch();

}

@Override
public void resize(int width, int height) {
    cam = new OrthographicCamera();
    cam.setToOrtho(false, width, height);
    cam.translate(width / 2, height / 2, 0);
    inputProcessor = new AttackInputProcessor(width, height, cam);
    Gdx.input.setInputProcessor(inputProcessor);

}

@Override
public void render() {

    Gdx.gl.glClearColor(0,0,0,1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
    batch.setProjectionMatrix(cam.combined);
    batch.begin();
    batch.draw(backgroundTexture, 0, 0, 2400, 460);
    batch.end();

}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    backgroundTexture.dispose();

}

}

そしてInputProcessorで

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {

    cam.position.set(screenX, posY / 2, 0);
    cam.update();
    return false;
}

この質問LibGdx How to Scroll using OrthographicCamera?の助けを借りて、ここまで来ました。. しかし、それは私の問題を本当に解決しません。

問題は touchDragged corodinates がワールド座標ではないことにあると思いますが、カメラの投影を解除しようとしましたが、効果はありませんでした。

私はこれに数週間苦労しており、これについて何か助けていただければ幸いです.

前もって感謝します。

4

5 に答える 5

7

私は最近あなたが望むように何かをしました。これは、マップの移動に使用するInputクラスです。変更する必要があるのは、「cam」の「stage.getCamera()」だけです。

public class MapInputProcessor implements InputProcessor {
    Vector3 last_touch_down = new Vector3();

    ...

    public boolean touchDragged(int x, int y, int pointer) {
        moveCamera( x, y );     
        return false;
    }

    private void moveCamera( int touch_x, int touch_y ) {
        Vector3 new_position = getNewCameraPosition( touch_x, touch_y );

        if( !cameraOutOfLimit( new_position ) )
            stage.getCamera().translate( new_position.sub( stage.getCamera().position ) );

        last_touch_down.set( touch_x, touch_y, 0);
    }

    private Vector3 getNewCameraPosition( int x, int y ) {
        Vector3 new_position = last_touch_down;
        new_position.sub(x, y, 0);
        new_position.y = -new_position.y;
        new_position.add( stage.getCamera().position );

        return new_position;
    }

    private boolean cameraOutOfLimit( Vector3 position ) {
        int x_left_limit = WINDOW_WIDHT / 2;
        int x_right_limit = terrain.getWidth() - WINDOW_WIDTH / 2;
        int y_bottom_limit = WINDOW_HEIGHT / 2;
        int y_top_limit = terrain.getHeight() - WINDOW_HEIGHT / 2;

        if( position.x < x_left_limit || position.x > x_right_limit )
            return true;
        else if( position.y < y_bottom_limit || position.y > y_top_limit )
            return true;
        else
          return false;
}


    ...
}

結果は次のとおりです。http ://www.youtube.com/watch?feature = player_embedded&v = g1od3YLZpww

于 2013-03-03T20:27:37.287 に答える
1

コールバックでさらに多くの計算を行う必要がありtouchDraggedます。タッチされた画面座標をカメラに渡すことはできません。ユーザーが指をドラッグした距離と方向把握する必要があります。絶対座標はすぐには役に立ちません。

右上から下にドラッグするか、左上から下にドラッグすることを検討してください。どちらの場合も、カメラを同じ距離だけ移動させたい (と思います) が、画面座標の絶対値は 2 つの場合で大きく異なります。

最も簡単な方法は、単に追跡previousXし、previousY(メソッドでそれらを初期化します。次に、デルタ ( 、たとえば) を使用してtouchDown呼び出すことです。また、 in を更新します。cam.translate()deltaX = screenX - previousXtouchDraggedprevious*touchDragged

InputProcessorまたは、 libgdx が提供するより洗練されたラッパーのいくつかを見ることができます ( https://code.google.com/p/libgdx/wiki/InputGestureDetectionを参照)。

于 2013-02-27T20:24:55.510 に答える
1

簡単な答え:

新しいドラッグ位置と古いドラッグ位置を保持する 2 つのフィールドを宣言します。

Vector2 dragOld, dragNew;

触れたばかりのときに、これらの両方を触れた場所に等しく設定しないと、カムがジャンプします。

if (Gdx.input.justTouched())
{
    dragNew = new Vector2(Gdx.input.getX(), Gdx.input.getY());
    dragOld = dragNew;
}

フレームごとに dragNew を更新し、ベクトルを互いに減算して、カメラを移動するための x と y を取得します。

if (Gdx.input.isTouched())
    {
        dragNew = new Vector2(Gdx.input.getX(), Gdx.input.getY());
        if (!dragNew.equals(dragOld))
        {
            cam.translate(dragOld.x - dragNew.x, dragNew.y - dragOld.y); //Translate by subtracting the vectors
            cam.update();
            dragOld = dragNew; //Drag old becomes drag new.
        }
    }

オルソ カムをドラッグするのに使用するのはこれだけです。シンプルで効果的です。

于 2014-10-19T11:28:41.727 に答える
0

私はそれを自分で使用したことはありませんが、次のコードを見ることから始めます。

http://libgdx.l33tlabs.org/docs/api/com/badlogic/gdx/scenes/scene2d/ui/FlickScrollPane.htmlのコードを見ましたか?

見る:touchDragged

于 2013-02-27T15:03:17.983 に答える