【问题标题】:detecting touch on specific area in Flappy bird clone检测 Flappy Bird 克隆中特定区域的触摸
【发布时间】:2014-07-09 18:43:12
【问题描述】:

所以,我正在制作一个飞扬的鸟克隆。问题是我是使用 java 和 libgdx 编程的新手,我想请你帮忙。我想在特定区域(只是一个简单的矩形)上进行触摸检测,而不是在整个屏幕上单击。

这是我当前输入处理程序类的代码:

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

}

【问题讨论】:

  • 您最好按照教程进行操作,而不是指望有人填写点点滴滴。一切顺利。

标签: java android libgdx flappy-bird-clone


【解决方案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(new Point(TouchXCoord, TouchYCoord));

【讨论】:

    【解决方案2】:

    你得到了 screenX 和 screenY,所以最简单的方法就是硬编码一个矩形的坐标,你检查触摸坐标。如果你想在物理上显示矩形,那么你必须在绘制屏幕时这样做。

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-12
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多