3

ゲーム開発は初めての試みです。libgdx の実験を開始し、ゲーム プログラミングのさまざまな側面を理解し始めました。サンプル プロジェクトを見たところ、libgdx ゲームの全体的なアーキテクチャを理解できました。しかし、ゲームのダイナミクスの基本を理解するために、単純な形状の描画方法、それらの移動方法、衝突の処理方法などの低レベルのものから始めました。

だから私は死んだシンプルなアンドロイドゲームを書くことを計画しました(確かにゲームでさえありません)。これがアイデアです

1. Create random shapes and make it fly (move)
2. When user touches the shape, it ll explode or hide or play simple animation
3. Has to show Hit & Miss count

最初は、libgdx のステージとアクターのコンセプトを試してみることを考えていましたが、シーン API なしで行うことはできませんでした。そして、基本的なゲームのさまざまな側面を試し、libgdx の背後にある概念をよりよく理解するために、これを開始しました。だから私はこのシンプルなアプリを作りました。オブジェクトをランダムに落下させることができます。

public class A1GameScreen implements Screen {

    OrthographicCamera camera;
    ShapeRenderer debugRenderer = new ShapeRenderer();
    Array<Rectangle> boxes;   
    long lastFlew;
    int fliesCaught;

    A1GameScreen(){

         camera = new OrthographicCamera();
         camera.setToOrtho(false, 800, 480);       
         boxes=new Array<Rectangle>();
         throwboxes();
    }
@Override
public void render(float delta) {

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


          camera.update();


    debugRenderer.setProjectionMatrix(camera.combined);
    debugRenderer.begin(ShapeType.Line);

    for (Rectangle fly : boxes) {
        debugRenderer.rect(fly.x, fly.y, fly.width, fly.height);
    }
    debugRenderer.end();

    //Handle the user input

    if (Gdx.input.isTouched()){          
        hit(Gdx.input.getX(),Gdx.input.getY());
    }

    if (TimeUtils.nanoTime() - lastFlew > 1000000000)
        throwboxes();


    Iterator<Rectangle> iter = boxes.iterator();
    while (iter.hasNext()) {
        Rectangle fly = iter.next();        
        fly.x -= 2;
        if (fly.x + 32 < 0)
            iter.remove();

    }

}

//Method to create flies at random interval
private void throwBoxes(){

    Rectangle fly = new Rectangle();
    fly.y = MathUtils.random(0, 480 - 32);
    fly.x = 800;
    fly.width = 32;
    fly.height = 32;
    boxes.add(fly);

    lastFlew = TimeUtils.nanoTime();

}

private boolean hit (float x, float y) {
    boolean found=false;

    for (Rectangle fly : boxes) {
        found = fly.contains(x,y);
        if (found){
            found = true;
            fly.width=100;
            break;
        }


    }

    return found;

}

}

しかし、落下物からタッチしたアイテムを見つけることができませんでした。これは、ボックスがタッチ範囲内にあるかどうかを調べるために行っていることです

  1. 配列内のすべてのボックスをループします
  2. タッチ座標がボックス座標内にあるかどうかを確認します
  3. それを理解するためcontainsに、(ボックス)のメソッドにタッチ座標を渡し ましたRectangle

このようなもの

  for (Rectangle fly : boxes) {
        found = fly.contains(x,y);
        if (found){
            found = true;
            fly.width=100;
            break;
        }


    }

しかし、うまくいきません。私は問題を理解したと思います。これは

  1. ボックスはフレームごとに x 軸で 2 ピクセル移動し、飛行効果を出します。
  2. しかし、タッチイベントからいくつかのフレームが経過したと仮定しています。それが私が期待した結果を得ていない理由です

私の質問は

  1. この問題を解決するには?
  2. libgdxで衝突を検出する最良の方法は何ですか?

アップデート

タッチ座標とボックス座標の間に多くの不一致が見られます。タッチ範囲内のボックスはありません。なんとそれは可能です。サンプルトレースの下

Touch coords: x-651.0 y-362.0

Fly coords: x-384.0 y-277.0
Fly coords: x-504.0 y-34.0
Fly coords: x-624.0 y-103.0
Fly coords: x-744.0 y-238.0



Touch coords: x-441.0 y-193.0

Fly coords: x-52.0 y-34.0
Fly coords: x-172.0 y-103.0
Fly coords: x-292.0 y-238.0
Fly coords: x-414.0 y-261.0
Fly coords: x-534.0 y-109.0
Fly coords: x-656.0 y-283.0
Fly coords: x-776.0 y-323.0


Touch coords: x-568.0 y-162.0

Fly coords: x-42.0 y-267.0
Fly coords: x-162.0 y-166.0
Fly coords: x-282.0 y-266.0
Fly coords: x-404.0 y-52.0
Fly coords: x-526.0 y-296.0
Fly coords: x-646.0 y-64.0
Fly coords: x-766.0 y-16.0
4

2 に答える 2

1

タップ位置に続く非表示の長方形を作成することで問題を解決し、非表示の長方形の重なりをチェックし、画面がタップされたかどうかをチェックするifステートメントを作成しました。

コードは次のとおりです。

if(Gdx.input.isTouched()) {
        Vector3 pointerPos = new Vector3();
        pointerPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
        cam.unproject(pointerPos);
        touchPos.x = pointerPos.x /*+ 64 / 2*/;
        touchPos.y = pointerPos.y /*+ 64 / 2*/;
    }

    Iterator<Rectangle> iter = raindrops.iterator();
    while(iter.hasNext()) {
        Rectangle raindrop = iter.next();
        raindrop.y -= 300 * Gdx.graphics.getDeltaTime();
        if(raindrop.y + 64 < 0) {
            iter.remove();
        }
        if(raindrop.overlaps(touchPos) && Gdx.input.justTouched()) {
            iter.remove();
            dropSound.play();
        }

このコードはすべて render メソッドにあります。

PS: 雨粒を必要なフライ オブジェクトに変更します。

于 2014-07-30T00:05:28.617 に答える