真的卡在 Android/Java 上了。触摸 Sprite 按钮上的事件以执行特定操作
Really stuck on Android/Java. Touch Event on a Sprite button to perform specific action
在过去一天左右的时间里,我真的一直坚持这个问题,我真的可以利用别人的意见来判断我哪里出错了
所以我有一张 'Play Game' 图片,我正在将它加载到我的游戏中,如下所示:
playGame = new SimpleControl(250.0f, 225.0f, 140.0f, 30.0f, "PlayBtn", this);
SimpleControl 对象如下所示:
public SimpleControl(float x, float y, float width, float height,
String bitmapName, GameScreen gameScreen) {
super(x, y, width, height, gameScreen.getGame().getAssetManager()
.getBitmap(bitmapName), gameScreen);
public boolean isActivated() {
// Consider any touch events occurring in this update
Input input = mGameScreen.getGame().getInput();
// Check if any of the touch events were on this control
BoundingBox bound = getBound();
for (int idx = 0; idx < TouchHandler.MAX_TOUCHPOINTS; idx++) {
if (input.existsTouch(idx)) {
if (bound.contains(input.getTouchX(idx), input.getTouchY(idx))) {
return true; }}}
return false; }
我的游戏 class 中有一个更新方法是这样的:
Input input = mGame.getInput();
List<TouchEvent> touchEvents = input.getTouchEvents();
if (touchEvents.size() > 0) {
TouchEvent touchevent = touchEvents.get(0);
{
if (playGame.isActivated()) {
mGame.getScreenManager().removeScreen(this.getName());
AboutScreen aboutScreen = new AboutScreen(mGame);
mGame.getScreenManager().addScreen(aboutScreen);
}
不幸的是,当我 运行 这个时,按钮不会接收任何触摸事件,但屏幕的一小部分会执行以下操作:
http://i.imgur.com/R4nVIRP.png
BoundingBox.java:
public BoundingBox(float x, float y, float halfWidth, float halfHeight) {
this.x = x;
this.y = y;
this.halfWidth = halfWidth;
this.halfHeight = halfHeight;
}
public boolean contains(float x, float y) {
return (this.x - this.halfWidth < x && this.x + this.halfWidth > x
&& this.y - this.halfHeight < y && this.y + this.halfHeight > y);
}
提前谢谢你:)
您的 contains(float x, float y)
看起来不正确。
this.x - this.halfwidth < x
表示它在边界框开始之前的位置检查 x
。
例如你的 x 应该是 -
(x>=this.x && x<=(this.x + 2*this.halfwidth))
你应该是
(y>=this.y && y<=(this.y + 2.this.halfHeight))
(假设 halfWidth 和 halfHeight 是他们说的)
(另外,无法打开 imgur link,因为它已为我们屏蔽)
在过去一天左右的时间里,我真的一直坚持这个问题,我真的可以利用别人的意见来判断我哪里出错了
所以我有一张 'Play Game' 图片,我正在将它加载到我的游戏中,如下所示:
playGame = new SimpleControl(250.0f, 225.0f, 140.0f, 30.0f, "PlayBtn", this);
SimpleControl 对象如下所示:
public SimpleControl(float x, float y, float width, float height,
String bitmapName, GameScreen gameScreen) {
super(x, y, width, height, gameScreen.getGame().getAssetManager()
.getBitmap(bitmapName), gameScreen);
public boolean isActivated() {
// Consider any touch events occurring in this update
Input input = mGameScreen.getGame().getInput();
// Check if any of the touch events were on this control
BoundingBox bound = getBound();
for (int idx = 0; idx < TouchHandler.MAX_TOUCHPOINTS; idx++) {
if (input.existsTouch(idx)) {
if (bound.contains(input.getTouchX(idx), input.getTouchY(idx))) {
return true; }}}
return false; }
我的游戏 class 中有一个更新方法是这样的:
Input input = mGame.getInput();
List<TouchEvent> touchEvents = input.getTouchEvents();
if (touchEvents.size() > 0) {
TouchEvent touchevent = touchEvents.get(0);
{
if (playGame.isActivated()) {
mGame.getScreenManager().removeScreen(this.getName());
AboutScreen aboutScreen = new AboutScreen(mGame);
mGame.getScreenManager().addScreen(aboutScreen);
}
不幸的是,当我 运行 这个时,按钮不会接收任何触摸事件,但屏幕的一小部分会执行以下操作: http://i.imgur.com/R4nVIRP.png
BoundingBox.java:
public BoundingBox(float x, float y, float halfWidth, float halfHeight) {
this.x = x;
this.y = y;
this.halfWidth = halfWidth;
this.halfHeight = halfHeight;
}
public boolean contains(float x, float y) {
return (this.x - this.halfWidth < x && this.x + this.halfWidth > x
&& this.y - this.halfHeight < y && this.y + this.halfHeight > y);
}
提前谢谢你:)
您的 contains(float x, float y)
看起来不正确。
this.x - this.halfwidth < x
表示它在边界框开始之前的位置检查 x
。
例如你的 x 应该是 -
(x>=this.x && x<=(this.x + 2*this.halfwidth))
你应该是
(y>=this.y && y<=(this.y + 2.this.halfHeight))
(假设 halfWidth 和 halfHeight 是他们说的) (另外,无法打开 imgur link,因为它已为我们屏蔽)