调用函数时如何循环此 if 语句
How can I loop this if statement when I call a function
我想在每次计数器为 10,20,30,40...(可被 10 整除)时调用 boss[i].showBoss()
和 .moveBoss()
函数,( if(counter % 10 === 0)
有效只有当计数器的数字可以被 10 整除时,而不是其他数字),但是这个硬编码示例只在 counter == 10
之后运行代码一次,而不是在 counter == 20,30,40
等时运行代码。关于我如何能的任何建议可以在每次 counter % 10 == 0
时启动功能,但在计数器不是 % 10
后不能停止它们,例如 11?
function draw() {
// put drawing code here
background(220);
if (counter >= 10) {
for(i = 0; i < boss.length; i++){
boss[i].showBoss();
boss[i].moveBoss();
}
} else if (counter >= 20) {
for(i = 0; i < boss.length; i++){
boss[i].showBoss();
boss[i].moveBoss();
}
} else if (counter >= 30) {
for(i = 0; i < boss.length; i++){
boss[i].showBoss();
boss[i].moveBoss();
}
}
}
你在这里是在回答你自己的问题...
你就不能这样做吗:
function draw() {
// put drawing code here
background(220);
if (counter % 10 === 0) {
for (i = 0; i < boss.length; i++) {
boss[i].showBoss();
boss[i].moveBoss();
}
}
}
函数 draw()
无论如何都会 运行。你需要做的是检查你的计数器何时可以被 10 整除,你的指令 counter % 10 === 0
可以正常工作。在这里我用 setInterval
模仿了绘制函数的行为。请注意,draw 函数现在是一个箭头函数,可以访问作用域中的 counter 变量。这与你的情况无关。
let counter = 0;
let draw = () => {
// we do normal draw things
// background(255);
if(counter % 10 === 0) {
// we need to animate the boss.
console.log('current counter was divisible by 10', counter);
}
counter ++;
};
setInterval(draw, 100)
创建一个对象来表示您要启动的 Boss 操作。当被 10 整除时,创建其中一个并添加到要绘制的老板列表中。每个绘制循环,绘制你所有的老板。
let bossesToDraw = [];
function draw(){
if(counter % 10 == 0){
bosses.push({
// state for the boss like its current position
// this could also create a new boss if you have a proper object
});
}
bosses.forEach(function(boss){
boss.showBoss();
boss.moveBoss()
});
//maybe check if you should remove the boss
}
我想在每次计数器为 10,20,30,40...(可被 10 整除)时调用 boss[i].showBoss()
和 .moveBoss()
函数,( if(counter % 10 === 0)
有效只有当计数器的数字可以被 10 整除时,而不是其他数字),但是这个硬编码示例只在 counter == 10
之后运行代码一次,而不是在 counter == 20,30,40
等时运行代码。关于我如何能的任何建议可以在每次 counter % 10 == 0
时启动功能,但在计数器不是 % 10
后不能停止它们,例如 11?
function draw() {
// put drawing code here
background(220);
if (counter >= 10) {
for(i = 0; i < boss.length; i++){
boss[i].showBoss();
boss[i].moveBoss();
}
} else if (counter >= 20) {
for(i = 0; i < boss.length; i++){
boss[i].showBoss();
boss[i].moveBoss();
}
} else if (counter >= 30) {
for(i = 0; i < boss.length; i++){
boss[i].showBoss();
boss[i].moveBoss();
}
}
}
你在这里是在回答你自己的问题...
你就不能这样做吗:
function draw() {
// put drawing code here
background(220);
if (counter % 10 === 0) {
for (i = 0; i < boss.length; i++) {
boss[i].showBoss();
boss[i].moveBoss();
}
}
}
函数 draw()
无论如何都会 运行。你需要做的是检查你的计数器何时可以被 10 整除,你的指令 counter % 10 === 0
可以正常工作。在这里我用 setInterval
模仿了绘制函数的行为。请注意,draw 函数现在是一个箭头函数,可以访问作用域中的 counter 变量。这与你的情况无关。
let counter = 0;
let draw = () => {
// we do normal draw things
// background(255);
if(counter % 10 === 0) {
// we need to animate the boss.
console.log('current counter was divisible by 10', counter);
}
counter ++;
};
setInterval(draw, 100)
创建一个对象来表示您要启动的 Boss 操作。当被 10 整除时,创建其中一个并添加到要绘制的老板列表中。每个绘制循环,绘制你所有的老板。
let bossesToDraw = [];
function draw(){
if(counter % 10 == 0){
bosses.push({
// state for the boss like its current position
// this could also create a new boss if you have a proper object
});
}
bosses.forEach(function(boss){
boss.showBoss();
boss.moveBoss()
});
//maybe check if you should remove the boss
}