Flash 用人工智能复制敌人

Flash 用人工智能复制敌人,flash,variables,actionscript-2,Flash,Variables,Actionscript 2,我正在制作一个flash游戏,你可以在其中与敌人战斗。我做了整个人工智能的敌人,我称之为粘液。现在我想把这个敌人的多个放在现场,我想知道我是否必须复制所有的代码,例如slime1 slime2等等 if ((img_background.BackGround.wall).hitTest(slime._x + radius, slime._y,true )) { // When the slime hits a right wall slime._x -= 8 } if ((img_ba

我正在制作一个flash游戏,你可以在其中与敌人战斗。我做了整个人工智能的敌人,我称之为粘液。现在我想把这个敌人的多个放在现场,我想知道我是否必须复制所有的代码,例如slime1 slime2等等

if ((img_background.BackGround.wall).hitTest(slime._x + radius, slime._y,true )) { // When the slime hits a right wall
    slime._x -= 8
}

if ((img_background.BackGround.wall).hitTest(slime._x, slime._y + radius, true)) {
    slime._y -= 8;
}
if ((img_background.BackGround.wall).hitTest(slime._x, slime._y - radius, true)) {
    slime._y += 8;
}
if ((img_background.BackGround.wall).hitTest(slime._x - radius, slime._y, true)) {
    slime._x += 8;
}

if ((img_background.BackGround.wall).hitTest(slime._x)){
    SLIwalltouch = 1
}else{
    SLIwalltouch = 0
}
我是否可以指定一个具有多个值的变量,并执行类似“slime”+numberofslimes…..的操作。。。。。 我是新手,我需要帮助。谢谢


注意:我正在使用flash actionscript 2.0

使用函数包装代码,参数将是您想要的任何movieclip。首先,让我们准备一个函数,它将根据“目标”检查命中测试。例如,我们称之为checkTouch

    function checkTouch(target){
      // When the slime hits a right wall
      if ((img_background.BackGround.wall).hitTest(target._x + radius, target._y,true )){ 
        target._x -= 8
      }
      if ((img_background.BackGround.wall).hitTest(target._x, target._y + radius, true)) {
        target._y -= 8;
      }
      if ((img_background.BackGround.wall).hitTest(target._x, target._y - radius, true)) {
        target._y += 8;
      }
      if ((img_background.BackGround.wall).hitTest(target._x - radius, target._y, true)) {
        target._x += 8;
      }

      if ((img_background.BackGround.wall).hitTest(target._x)){
        SLIwalltouch = 1
      }else{
        SLIwalltouch = 0
      }
    }
请注意,我已将所有出现的“slime”替换为“target”。此时,您可以使用您喜欢的任何movieclip调用该函数,它将检查该movieclip的命中率

要使代码与您的代码完全相同,只需添加以下内容:

    checkTouch(slime);
现在,这应该是关于如何检查更多对象的提示。比如说,如果你有被称为“slime”、“slime2”、“hellhound”和“dragon”的敌人,你可以这么做

    checkTouch(slime);
    checkTouch(slime2);
    checkTouch(hellhound);
    checkTouch(dragon);
但是,如果您最终拥有更多的敌人,那么将敌人添加到阵列中,然后在循环中对其应用该函数将非常有用:

    var all_enemies=[slime, slime2, hellhound, mushroom, ..., enemyN];

    for(enemy in all_enemies){
      checkTouch(enemy);
    }
请注意,为了发挥作用,敌人必须先出现在舞台上,然后才能像这样在阵列中声明

如果你不想把所有的敌人都写出来,并且知道你将拥有的敌人的确切数量,你可以利用flash的能力来构建movieclip名称

它是这样的,命名你所有的敌人,比如enemy1,enemy2,enemy3。。。 然后使for循环如下所示:

    for(i=0; i<numberofenemies; i++){ //replace numberofenemies with the number of enemies
      checkTouch(this["enemy"+i]);
    }

用于(i=0;iis slime)电影剪辑?