MakeCode JS 中是否有 "repeat [function] until [property = true]" 类型的循环?
Is there a "repeat [function] until [property = true]" type of loop in MakeCode JS?
我正在 Microsoft MakeCode Arcade 中为一个学校项目制作游戏,我想知道是否有像那里那样的“重复 [function] 直到 [属性 = true]”类型的循环在卢奥。我想使用它,以便游戏等到我的玩家精灵位于 运行 某些代码的特定坐标。我想出了一种以不同方式执行此操作的方法,但我想知道以供将来参考。
如果有人想知道,这就是我正在使用的替代方法。
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)
}
}
}
})
您可以使用 while
循环或 do...while
循环
对于while
循环,只要条件为真,下面的代码就会一直运行ning。
let x = 0
while (x < 3) {
x++
}
console.log(x) // print 3
对于do...while
循环,只要条件为真,下面的代码就会一直运行ning。这个循环将 运行 至少一次。
let result = '';
let x = 0;
do {
x = x + 1;
result = result + x;
} while (x < 5);
console.log(result); // print "12345"
回到你的例子,我相信你 运行 每隔 100ms
循环一次(基于你的 game.onUpdateInterval
.
的第一个参数
您可以通过添加 timer
函数并将此循环包装为异步函数来轻松做到这一点。
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();
虽然我不是 100% 确定您当前解决方法的功能,但这是我的解释(希望它有效)
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();
我正在 Microsoft MakeCode Arcade 中为一个学校项目制作游戏,我想知道是否有像那里那样的“重复 [function] 直到 [属性 = true]”类型的循环在卢奥。我想使用它,以便游戏等到我的玩家精灵位于 运行 某些代码的特定坐标。我想出了一种以不同方式执行此操作的方法,但我想知道以供将来参考。
如果有人想知道,这就是我正在使用的替代方法。
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)
}
}
}
})
您可以使用 while
循环或 do...while
循环
对于while
循环,只要条件为真,下面的代码就会一直运行ning。
let x = 0
while (x < 3) {
x++
}
console.log(x) // print 3
对于do...while
循环,只要条件为真,下面的代码就会一直运行ning。这个循环将 运行 至少一次。
let result = '';
let x = 0;
do {
x = x + 1;
result = result + x;
} while (x < 5);
console.log(result); // print "12345"
回到你的例子,我相信你 运行 每隔 100ms
循环一次(基于你的 game.onUpdateInterval
.
您可以通过添加 timer
函数并将此循环包装为异步函数来轻松做到这一点。
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();
虽然我不是 100% 确定您当前解决方法的功能,但这是我的解释(希望它有效)
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();