【问题标题】:Add the height of an activity to an if statement将活动的高度添加到 if 语句
【发布时间】:2017-05-22 13:51:41
【问题描述】:

我正在尝试使用测量的高度并将其添加到我的 if 语句中,以便当“blueY”触摸屏幕底部时,它会导致游戏结束。不确定是在获取高度时出错还是在 if 语句中出错。

我的if语句如下:

    if (blueY >= height) {
        // Game Over
        timer.cancel();
        timer = null;

        Intent intent = new Intent(getApplicationContext(), Results.class);
        intent.putExtra("SCORE", score);
        startActivity(intent);

    }

在 onCreate 中:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    height = mFrame.getMeasuredHeight() -  startLabel.getMeasuredHeight() ;
    // I have also tried this :height  = displayMetrics.heightPixels;
    width = displayMetrics.widthPixels;

我对 Java 很陌生,如果这听起来很愚蠢,很抱歉。

【问题讨论】:

  • "我假设我需要获取屏幕的高度才能使其正常工作" -- 不,您需要获取游戏区域的高度(例如,无论 View you用于渲染游戏)。在很多情况下,这不是屏幕的高度。

标签: java android android-studio


【解决方案1】:

这是重复的问题!

    Display display = ((WindowManager) this.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    int mWidthScreen = display.getWidth();
    int mHeightScreen = display.getHeight();

【讨论】:

    【解决方案2】:

    我找到了当图像触及屏幕底部时使游戏结束(计时器停止)的解决方案:

      if (blueY >= frameHeight-blueSize) {
            // Game Over
            timer.cancel();
            timer = null;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-03
      相关资源
      最近更新 更多