简体   繁体   English

如何在此方案中添加for循环?

[英]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_mcbon3_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.

相关问题 如何实现动态 for 循环? - How can I achieve dynamic for-loop? 如何使Sublime Text的“改进的本机for循环”增量像普通的for循环一样? - How can I make Sublime Text's “Improved Native for-loop” increment like a normal for-loop? 如何优化一系列特定条件的for循环? - How can I optimize a for-loop over a list of specific conditions? 如何使用变量内的变量进行for循环工作 - how can I make an for-loop work with a variable inside a variable 如何在$(document).ready(function(){})中使用for循环? - How can I use a for-loop within a $(document).ready(function(){})? 如何在样式组件中使用for循环? - How can i use for-loop in styled-components? 在 javascript 中没有嵌套 for 循环的情况下如何解决这种情况 - How can i solve this scenario without nested for loop in javascript 添加在for循环中时如何添加到变量 - How to add to a variable when addition is in an for-loop 如何执行一个 for-loop function,然后在 for-loop function 完成一个按钮后调用另一个 function? - How can I execute one for-loop function, then call another function after the for-loop function is complete with a button? 如何在 for 循环中使用 I-Value - How to use I-Value in for-loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM