简体   繁体   中英

Want to increment 1 level for every 100 points scored, level counter not working

Basically, I want to increment 1 "level" every time 100 "points" are scored. The points work fine, but the levels will not increment for every 100 points. The problem exists in this small bit of code. Any ideas?

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

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

Here are my displayPoints and displayLevels functions

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

        };

Maybe you should check if modulus of _total_points and 100 are zero? I made an example for loop to demonstrate it

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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