0

So, I am making a flappy bird clone. The thing is that I am new in programming with java and libgdx, and I would kindly like to ask you for your help. I would like to make a touch detection on a specific area (just a simple rectangle shape), instead of clicking all over the screen.

Here is my current code from InputHandler class:

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;
}

}

  • You'd be better served by following along a tutorial than expecting someone to fill in the bits and pieces. All the best. – nikhil Jul 08 '14 at 17:42

2 Answers2

1

Create a method that returns true if the touch is within bounds of a rectangle.

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;
}

You can also add a method like this to the rectangle class itself so you can just make a call to the rectangle, something like this: rectangle.

ContainsPoint(new Point(TouchXCoord, TouchYCoord));

Madmenyo
  • 8,389
  • 7
  • 52
  • 99
0

You are given screenX and screenY, so the easiest way is just hardcode coordinates for a rectangle that you check the touch coordinates against. If you physically want to show the rectangle then you have to do that when the screen is drawn.

RScottCarson
  • 980
  • 5
  • 20