gpt4 book ai didi

java - 检测 Flappy Bird 克隆中特定区域的触摸

转载 作者:行者123 更新时间:2023-12-01 12:46:40 26 4
gpt4 key购买 nike

所以,我正在制作一只飞翔的小鸟克隆。问题是我是使用 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));

关于java - 检测 Flappy Bird 克隆中特定区域的触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24638299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com