[英]How can I add a for-loop in this scenario?
In my code, if I collide with bon1_mc
, it adds points to my counter and plays a sound. 在我的代码中,如果我与bon1_mc
发生碰撞,它会向我的计数器添加点数并播放声音。 Also if I collide 5 times with bon1_mc
it stops the game and plays a soundtrack. 此外,如果我与bon1_mc
碰撞5次,它将停止游戏并播放配乐。 How can I make it so it does the same thing with bon2_mc
and bon3_mc
(these are clips from my animate project)? 我怎样才能做到这一点,它与bon2_mc
和bon3_mc
(这些是我的动画项目中的剪辑)做同样的事情? I know I can use a for-loop, but I don't know how to incorporate it. 我知道我可以使用for循环,但我不知道如何合并它。
function fCollision(ennemi) {
let collision = ndgmr.checkRectCollision(exportRoot.jeu_mc.moi_mc, ennemi);
if (collision) {
if (ennemi === exportRoot.jeu_mc.bon1_mc) { // Action si gagne
points++;
exportRoot.jeu_mc.points_txt.text = "Points : " + points;
playSound("Bonc");
if (points === 5) {
playSound("Victoire");
for (let x = 1; x <= 2; x++) {
exportRoot.jeu_mc["mauvais" + x + "_mc"].removeEventListener("tick", fBougeEnnemis);
}
for (let x = 1; x <= 3; x++) {
exportRoot.jeu_mc["bon" + x + "_mc"].removeEventListener("tick", fBougeBons);
}
document.removeEventListener("keydown", fQuelleTouche);
document.removeEventListener("keyup", annuleTouche);
}
}
}
}
Technically you don't have to do a loop, with your structure you can use an array of your objects then use array.indexOf
to check against the array. 从技术上讲,您不必进行循环,使用您的结构可以使用对象数组,然后使用array.indexOf
来检查数组。
blocks
is the array that I created from your objects, and I updated your if statement
. blocks
是我从你的对象创建的数组,我更新了你的if statement
。
function fCollision(ennemi) {
let blocks = [exportRoot.jeu_mc.bon1_mc,exportRoot.jeu_mc.bon2_mc,exportRoot.jeu_mc.bon3_mc];
let collision = ndgmr.checkRectCollision(exportRoot.jeu_mc.moi_mc, ennemi);
if (collision) {
if (blocks.indexOf(ennemi) > -1) { // Action si gagne
points++;
exportRoot.jeu_mc.points_txt.text = "Points : " + points;
playSound("Bonc");
if (points === 5) {
playSound("Victoire");
for (let x = 1; x <= 2; x++) {
exportRoot.jeu_mc["mauvais" + x + "_mc"].removeEventListener("tick", fBougeEnnemis);
}
for (let x = 1; x <= 3; x++) {
exportRoot.jeu_mc["bon" + x + "_mc"].removeEventListener("tick", fBougeBons);
}
document.removeEventListener("keydown", fQuelleTouche);
document.removeEventListener("keyup", annuleTouche);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.