javascript2d-gamesmakecode

Is there a "repeat [function] until [property = true]" type of loop in MakeCode JS?


I'm making a game in Microsoft MakeCode Arcade for a school project, and I wanted to know if there was a "repeat [function] until [property = true]" type of loop like there is in Luau. I wanted to use this so that the game waits until my player sprite is at a certain coordinate to run some code. I figured out a way to do this in a different way, but I wanted to know just for future reference.

If anyone is wondering, this is what the alternative way I am using.

game.onUpdateInterval(100, function () {
    if (level == 1) {
        if (myPlayer.x == 950 && myPlayer.y == 140) {
            myPlayer.y = 100
            myPlayer.x = 10
            if (game.ask("Does " + level_1 + " + " + level1_2 + " = " + level1CorrectAns + "?")) {
                console.log("Level 1 Completed successfully")
                level += 1
                LevelChange()
            } else {
                game.over(false)
            }
        }
    }
})

Solution

  • You could use either while loop or do...while loop

    For while loop, the following code will keep on running as long as the condition is true.

    let x = 0
    
    while (x < 3) {
      x++
    }
    
    console.log(x) // print 3
    

    For do...while loop, the following code will keep on running as long as the condition is true. And this loop will run at least once.

    let result = '';
    let x = 0;
    
    do {
      x = x + 1;
      result = result + x;
    } while (x < 5);
    
    console.log(result); // print "12345"
    

    Coming back to your example, I believe you're running the loop every 100ms (based on first argument of your game.onUpdateInterval.

    You could easily do this by adding a timer function and wrap this loop in as an async function.

    const timer = ms => new Promise(res => setTimeout(res, ms))
    
    async function updateInterval() {
      while () {
      // Your logic here
      await timer(100) // You can change the timeout to your desired ms
      }
    }
    
    updateInterval();
    

    While I'm not 100% sure of the functionality of your current workaround, but this is my interpretation (Hope it works)

    const timer = (ms) => new Promise((res) => setTimeout(res, ms));
    
    async function updateInterval() {
      let state = true; // This is just a condition if the loop should continue
      while (state) {
        if (level == 1) {
          if (myPlayer.x == 950 && myPlayer.y == 140) {
            myPlayer.y = 100;
            myPlayer.x = 10;
            if (
              game.ask(
                'Does ' +
                  level_1 +
                  ' + ' +
                  level1_2 +
                  ' = ' +
                  level1CorrectAns +
                  '?'
              )
            ) {
              console.log('Level 1 Completed successfully');
              level += 1;
              LevelChange();
              state = false; // Update the state to false, so it will exit the while loop
            } else {
              game.over(false);
            }
          }
        }
        await timer(100); // You can change the timeout to your desired ms
      }
    }
    
    updateInterval();