想要每100分增加1级,级别计数器不起作用

时间:2019-05-08 20:21:55

标签: javascript

基本上,我想每得分100分就增加1级。这些点工作正常,但级别不会每100点增加一次。这段代码中存在问题。有什么想法吗?

if (my.collisions()) {
                _total_points += 1;
                my.displayPoints();

                if (total_points >= 100) {
                  _total_levels += 1;
                  my.displayLevels();
                }
            }

这是我的displayPoints和displayLevels函数

        my.displayPoints = function () {
            gcontext.clearRect(0, 0, 200, 100);
            gcontext.fillStyle = "#ffffff";
            gcontext.fillRect(0, 0, 200, 100);
            gcontext.font = "30px Arial";
            gcontext.fillStyle = "#000000";
            gcontext.fillText("Score: " + _total_points, 10, 50);

        };

        my.displayLevels = function () {
            gcontext.clearRect(100, 100, 200, 100);
            gcontext.fillStyle = "#ffffff";
            gcontext.fillRect(0, 100, 200, 100);
            gcontext.font = "30px Arial";
            gcontext.fillStyle = "#000000";
            gcontext.fillText("Level: " + _total_levels, 10, 150);

        };

1 个答案:

答案 0 :(得分:0)

也许您应该检查_total_points的模数和100是否为零? 我以循环为例进行了演示

let _total_points = 0;
let _total_levels = 0;

for (let index = 0; index < 500; index++) {
  _total_points += 1;
  console.log("_total_points " + _total_points);
// if _total_points can be devided by 100, and remainder is zero
  if (_total_points % 100 === 0) {
    _total_levels += 1;

    console.log("_total_levels " + _total_levels);
  }
}