2 Replies Latest reply on Jun 15, 2007 5:30 AM by PXzyan

    help with AI

    PXzyan
      Hey guys,

      I am not a compleat noob at actionscripting but I have a problem here..very tricky and flash always ends up terminating the script during gamplay as it just does not work the way I think it should.

      Anyway. I will give you an example of what I am doing:

      I am making a game like Security 2. 2D overhead view where you are an agent going from room to room collecting objects and compleating objectives.

      So far I have managed to create 1 unit..and duplicate that to make many more enemys which makes things very easy.

      Now, I have the enemy spawn randomly around the map..max units are 7 and they will auto move to the left. Code looks like this:

      this._x = random(600)+100;
      this._y = random(480);
      this.speed = random(3)+1;

      now..I want it so when they meet a wall, they will rotate 180 degrees and move the other way...and t hen onec they hit another wall..they will rotate again and move that way!

      this code is long here..works perfectly atm...it spawns the units..tells them to move at a random speed and spawn randomly...if you can..please show me how to make it so..hey will turn and move the other way!!

      onClipEvent (load) {
      function reset() {
      if (this._name == "enemy") {
      this._visible = false;
      } else {
      this._visible = true;
      }
      this._x = random(600)+100;
      this._y = random(480);
      this.speed = random(3)+1;
      }
      this.reset();
      }
      onClipEvent (enterFrame) {
      this._x -= this.speed;
      if (this._x<-50) {
      this.reset();
      }
      }
      onClipEvent (load) {
      power = 3;
      radius = 6;
      }
      onClipEvent (enterFrame) {
      while (_root.wall.hitTest(_x-radius, _y, true)) {
      _x++;
      }
      while (_root.wall.hitTest(_x+radius, _y, true)) {
      _x--;
      }
      if ((_root.enemy.hitTest(_x, _y+radius, true)) or (_root.enemy.hitTest(_x, _y-radius, true)) or (_root.enemy.hitTest(_x+radius, _y, true)) or (_root.enemy.hitTest(_x-radius, _y, true))) {
      _x = 1000;
      _y = 1000;
      }
      }


      Thank you for listening and I hope you can give me a hand.
        • 1. Re: help with AI
          MaxManNH Level 2
          I may not have a total handle on your code but could you not state a deltax for your enemy and then once it hit a wall flip the deltax so it was going in the other direction?

          For instance.

          enemy.dx = 3;

          if(enemy hits a wall)
          {
          enemy.dx *= -1 //reverse enemy motion
          }
          • 2. Re: help with AI
            PXzyan Level 1
            hmm..I cant seem to add it! When I do add it to the code..it does nothing iam afriad :( the units just hit off the wall and stop. This is anoying now..ive tryed alot of different things and nothing will work...and yet..it seems to simple..