【问题标题】:If condition is not executing如果条件未执行
【发布时间】:2015-11-05 04:53:59
【问题描述】:

我正在尝试确定我的对象是否被击中,稍微宽容一点。但它不起作用。这是我的代码:

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;

【问题讨论】:

    标签: java function if-statement libgdx intersection


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2019-12-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-21
        • 2016-12-17
        • 2011-08-25
        相关资源
        最近更新 更多