6 Replies Latest reply on Apr 25, 2011 3:51 AM by LukeElChul

    AI: Turning Directions

    EranthisAconite

      This is a pretty complicated subject, but I'm hoping to make a two way direction moving AI "enemy." The movie clip has the character facing in one direction so when the player passes him, he stays in that direction, moving that way animated but moving towards the player on the opposite side.

      I need something that rotates him horizontally so he'll be facing the player and looks as though he's moving towards him.

      code so far: (bunny and fox are just inputs from a past game) (all code is in frame 1)

       

      function moveBunnies() {
          // loop through all objects looking for bunnies
          for(var i=objects.length-1;i>=0;i--) {
              if (objects[i].type == "bunny") {
                 
                  // move only bunnies on screen
                  if (Math.abs(objects[i].x-foxPos.x) < 275) {
                     
                      // move toward fox
                      if (foxPos.x < objects[i].x) {
                          var dx = -bunnySpeed;
                      } else if (foxPos.x > objects[i].x) {
                          var dx = bunnySpeed;
                      }
                     
                      // determine bounds for this bunny
                      bunnyBounds = determineBounds(objects[i]);
                     
                      // move only as far as bounds
                      if ((dx < 0) and (bunnyBounds.left > Math.abs(dx))) {
                          objects[i].x += dx;
                      } else if ((dx > 0) and (bunnyBounds.right > Math.abs(dx))) {
                          objects[i].x += dx;
                      }
                     
                      // see whether bunny is close enough to fox
                      if (distance(_root["object "+i],fox) < 30) {
                          setScore(-5);
                          fox.gotoAndPlay(25);
                      }
                      if (getTimer() > nextHitTime) {
                          nextHitTime = getTimer()+1000;
                      }
                  }
              }
          }
      }

       

      So far I have a code that turns the player's character left or right by keys but I'd like to know how to do this with code on AI.

      Also if anyone knows, There will be times where the player will be be above the AI so when he's under the player, he'll just stop instead of vibrating rapidly or (if it works) moving in both directions rapidly.

      Please help too. I have to have this game finished in less than 3 days.