如果条件没有执行

时间:2015-11-05 04:54:00

标签: java function if-statement libgdx intersection

我试图弄清楚我的物体是否受到了轻微的打击。但它没有用。这是我的代码:

public boolean isClicked(float x) {

    if (Gdx.input.isTouched() && Gdx.graphics.getHeight() - Gdx.input.getY() >= this.getY() && Gdx.graphics.getHeight() - Gdx.input.getY() <= this.getY() + this.getHeight() && this.getWidth() >= Gdx.input.getX() && x < this.getY() + this.getHeight() * 1.2f && x > this.getY() - this.getHeight() * .2f) {
        return true;
    }

    return false;

2 个答案:

答案 0 :(得分:1)

总是尝试以这种方式使用条件并使用括号表示多种条件:

if (Gdx.input.isTouched() && ((Gdx.graphics.getHeight() - Gdx.input.getY()) >= this.getY()) && ((Gdx.graphics.getHeight() - Gdx.input.getY()) <= (this.getY() + this.getHeight())) && (this.getWidth() >= Gdx.input.getX()) && (x < ((this.getY() + this.getHeight()) * 1.2f)) && (x > ((this.getY() - this.getHeight()) * .2f))) {
      return true;
}
return false;

答案 1 :(得分:1)

您应该尝试格式化长行以便于阅读并使用括号。

if (Gdx.input.isTouched() 
    && (Gdx.graphics.getHeight() - Gdx.input.getY()) >= this.getY() 
    && (Gdx.graphics.getHeight() - Gdx.input.getY()) <= (this.getY() + this.getHeight()) 
    && this.getWidth() >= Gdx.input.getX() 
    && x < (this.getY() + this.getHeight() * 1.2f )
    && x > (this.getY() - this.getHeight() * .2f ) 
   ) {
    return true;
}

这也可以让你注释掉任何中间行的开头(例如使用//)来隔离问题。