0

それで、ゆるい鳥のクローンを作っています。問題は、私が Java と libgdx を使ったプログラミングに慣れていないことです。あなたの助けをお願いしたいと思います。画面全体をクリックするのではなく、特定の領域 (単純な長方形) でタッチ検出を行いたいと考えています。

InputHandler クラスの現在のコードは次のとおりです。

public class InputHandler implements InputProcessor {
private Bird myBird;
private GameWorld myWorld;

// Ask for a reference to the Bird when InputHandler is created.
public InputHandler(GameWorld myWorld) {
    // myBird now represents the gameWorld's bird.
   this.myWorld = myWorld;
   myBird = myWorld.getBird(); }

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

    if (myWorld.isReady()) {
        myWorld.start();
    }

    myBird.onClick();

    if (myWorld.isGameOver() || myWorld.isHighScore()) {
        // Reset all variables, go to GameState.READ
        myWorld.restart();
    }

    return true;
}

@Override
public boolean keyDown(int keycode) {
    return false;
}

@Override
public boolean keyUp(int keycode) {
    return false;
}

@Override
public boolean keyTyped(char character) {
    return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
    return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    return false;
}

@Override
public boolean scrolled(int amount) {
    return false;
}

}

4

2 に答える 2

1

タッチが長方形の境界内にある場合に true を返すメソッドを作成します。

public boolean ContainsPoint(Rectangle rectangle, Point touch)
{
    if (touch.X > rectangle.X && touch.X < rectangle.X + rectangle.Width &&
    touch.Y > rectangle.Y && touch.Y < rectangle.Y + rectangle.Height)
    return true;
    else
    return false;
}

このようなメソッドを長方形クラス自体に追加して、次のような長方形を呼び出すこともできます。

ContainsPoint(新しいポイント(TouchXCoord, TouchYCoord));

于 2014-07-08T18:12:44.277 に答える