繁体   English   中英

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

[英]How can I add a for-loop in this scenario?

在我的代码中,如果我与bon1_mc发生碰撞,它会向我的计数器添加点数并播放声音。 此外,如果我与bon1_mc碰撞5次,它将停止游戏并播放配乐。 我怎样才能做到这一点,它与bon2_mcbon3_mc (这些是我的动画项目中的剪辑)做同样的事情? 我知道我可以使用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);
      }
    }
  }
}

从技术上讲,您不必进行循环,使用您的结构可以使用对象数组,然后使用array.indexOf来检查数组。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM