简体   繁体   English

确实卡在Android / Java上。 在Sprite按钮上触摸事件以执行特定操作

[英]Really stuck on Android/Java. Touch Event on a Sprite button to perform specific action

I have been really stuck on this for the past day or so and I could really use somebody's input as to where I'm going wrong 在过去的一天左右的时间里,我真的一直被困在这个位置上,我真的可以使用别人的意见来确定我要去哪里

So I have a 'Play Game' image, I'm loading it into my game as follows: 因此,我有一个“玩游戏”图像,我将其按以下方式加载到我的游戏中:

playGame = new SimpleControl(250.0f, 225.0f, 140.0f, 30.0f, "PlayBtn", this);

The SimpleControl object looks like 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; }

I have an update method in my Game class which goes like this: 我的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);
            }

Unfortunately when I'm running this, the button does not pick up any touch event but there's a little section of the screen that does: http://i.imgur.com/R4nVIRP.png 不幸的是,当我运行此按钮时,该按钮没有发生任何触摸事件,但是屏幕上有一小部分显示了该事件: 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);
}

Thank you in advance :) 先感谢您 :)

Your contains(float x, float y) looks incorrect. 您的contains(float x, float y)看起来不正确。

this.x - this.halfwidth < x means it checks for x at a location before the start of the bounding box. this.x - this.halfwidth < x表示它在边界框开始之前的某个位置检查x

For example your x should be - (x>=this.x && x<=(this.x + 2*this.halfwidth)) And y should be (y>=this.y && y<=(this.y + 2.this.halfHeight)) 例如,您的x应该是- (x>=this.x && x<=(this.x + 2*this.halfwidth))并且y应该是(y>=this.y && y<=(this.y + 2.this.halfHeight))

(Assuming halfWidth and halfHeight are what they say) (also, can't open the imgur link since its blocked for us) (假设halfWidth和halfHeight是他们所说的话)(而且,由于它对我们而言已被阻止,因此无法打开imgur链接)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM