5 Replies Latest reply on Dec 21, 2012 12:46 PM by kglad

    How to make Enemy circle around object?

    LukeElChul

      Ok I have written a simple enemyAi code. I want so when the enemy hitTests the circle in the stage that it will circle around it and not go through it.

       

      Heres the code so far:

       

       

      onEnterFrame = function () {
                for (i=0; i<EnemyArray.length; i++) {
                          Render(_root.EnemyArray[i], _root.AI);
                          if (timer>0) {
                                    timer--;
                          }
                }
      };
      var AggroRange:Number = 250;
      var speed:Number = 5;
      var EnemyArray:Array = new Array();
      var Stop:Boolean = false;
      timer = 20;
      tag = 0;
      Spawn(400, 250);
      
      function Spawn(spawnx, spawny) {
                Enemys = _root.attachMovie("Enemy", "Enemy"+tag, tag, {_x:spawnx, _y:spawny});
                Enemys.health = 100;
                Enemys.Attack = false;
                Enemys.RandomMovex;
                Enemys.RandomMovey;
                EnemyArray.push(Enemys);
                tag++;
      }
      
      function Render(Enemy:MovieClip, Player:MovieClip) {
                          var diffX:Number = Enemy._x-Player._x;
                          var diffY:Number = Enemy._y-Player._y;
                          var distance:Number = Math.sqrt((diffY*diffY)+(diffX*diffX));
                          if (Attack == false && Stop == false) {
                                    if (distance<=AggroRange && distance>40) {
                                              Enemy._x -= Math.cos(Math.atan2(diffY, diffX))*speed;
                                              Enemy._y -= Math.sin(Math.atan2(diffY, diffX))*speed;
                                              Enemy._rotation = Math.atan2(diffY, diffX)/Math.PI*180;
                                    } else if (distance>AggroRange) {
                                              if (timer == 0) {
                                                        RandomMovex = Math.random()*100;
                                                        RandomMovey = Math.random()*100;
                                                        timer = 20;
                                              }
                                              if (RandomMovex<50) {
                                                        Enemy._x += speed-random(2)-2;
                                              } else if (RandomMovex>=50) {
                                                        Enemy._x -= speed-random(2)-2;
                                              }
                                              if (RandomMovey<50) {
                                                        Enemy._y += speed-random(2)-2;
                                              } else if (RandomMovey>=50) {
                                                        Enemy._y -= speed-random(2)-2;
                                              }
                                    }
                          }
                          if (distance<=40) {
                                    Attack = true;
                          } else if (distance>80) {
                                    Attack = false;
                          }
                          if (Attack == true) {
                                    Enemy.gotoAndStop("Attack");
                          } else {
                                    Enemy.gotoAndStop(1);
                          }
                          if(Enemy.hitTest(EnemyArray[i+1])){
                                    Stop = true;
                }else{
                          Stop = false;
      }
      }
      
      

       

      I was thinking of using the formula πr² to find the area of the circle then do some thing with this to go around this area:

      Enemy._x -= Math.cos(Math.atan2(diffY, diffX))*speed;

      but I dont know what to do Any sugestions?

        • 1. Re: How to make Enemy circle around object?
          kglad Adobe Community Professional & MVP

          i don't see anything about "the circle" in your code.  what is "the circle"?

          • 2. Re: How to make Enemy circle around object?
            LukeElChul Level 1

            The circle is on my stage but I dont know how to get the enemys to circle around it instead of going through it.

             

            if(EnemyArray[i].hitTest(_root.Circle)){

                                           circleArea = (_root.Circle._width*_root.Circle._width)*Math.PI/2;

                                                                         Stop = true;

                                                                         trace(circleArea)

                                                                         }

             

            This will be added on to the code and Ive teasted this and the hitTest works and I think I have calculated the area of the circle right.

             

            Now all I need to do is like have the enemy go around the circle on the stage, I dont know how to do this.

            • 3. Re: How to make Enemy circle around object?
              kglad Adobe Community Professional & MVP

              that's twice the area of a circle but the circle's area is irrelevant.  if you want the enemy to follow a path at the circle's circumference (and the circle's registration point is its center), then you want to move along:

               

               

              _x = _root.Circle._x+_root.Circle._width/2*Math.cos(angle);

              _y = _root.Circle._y+_root.Circle._width/2*Math.sin(angle);

              // where angle varies between then enemy's angle with the circle and Math.PI plus the enemy's angle with the circle.  the enemy's speed determines the angle increments.

              • 4. Re: How to make Enemy circle around object?
                LukeElChul Level 1

                Got it working Thanks !!!!

                • 5. Re: How to make Enemy circle around object?
                  kglad Adobe Community Professional & MVP

                  you're welcome.