所以,我正在制作一个飞扬的鸟克隆。事实是,我是使用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;
}
}
最佳答案
创建一个如果触摸在矩形范围内则返回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));