所以,我正在做一个脆弱的鸟类克隆。问题是我是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;
}
}
答案 0 :(得分: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));
答案 1 :(得分:0)
您将获得screenX和screenY,因此最简单的方法就是检查触摸坐标的矩形的硬编码坐标。如果你实际上想要显示矩形,那么你必须在绘制屏幕时这样做。