繁体   English   中英

如果条件未执行

[英]If condition is not executing

我试图弄清楚我的物体是否被击中,稍微宽容一点。 但它不起作用。 这是我的代码:

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;

始终尝试以这种方式使用条件并为多个条件使用括号:

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;

您应该尝试格式化长行以提高可读性并使用括号。

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;
}

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

暂无
暂无

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

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