[英]Really stuck on Android/Java. Touch Event on a Sprite button to perform specific action
在過去的一天左右的時間里,我真的一直被困在這個位置上,我真的可以使用別人的意見來確定我要去哪里
因此,我有一個“玩游戲”圖像,我將其按以下方式加載到我的游戲中:
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; }
我的Game類中有一個更新方法,如下所示:
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應該是(y>=this.y && y<=(this.y + 2.this.halfHeight))
(假設halfWidth和halfHeight是他們所說的話)(而且,由於它對我們而言已被阻止,因此無法打開imgur鏈接)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.