15 Replies Latest reply on Jun 17, 2010 10:37 PM by kglad

    How to limit the movement of my mc??

    dn0myar06 Level 1

      i have a movie clip moving randomly...but i would like to put a limit or boundary on its movement...

      the problem is...the bondary is a round area and i don't know how to calculate the area...

       

      is it possible to make a script that says...while inside this certain object, they will move randomly...

       

      as of now this is my code:

       

      var speed:Number = 4;                                             //this object shouldn't go beyond the boundary
      object_mc.onEnterFrame = function() {
      if (Key.isDown(Key.RIGHT)) {
      this._x = this._x+speed;
      } else if (Key.isDown(Key.LEFT)) {
      this._x = this._x-speed;
      }
      if (Key.isDown(Key.UP)) {
      this._y = this._y-speed;
      } else if (Key.isDown(Key.DOWN)) {
      this._y = this._y+speed;
      }
      };

       

      //////////////////////////////////////////////////////////////////////////////////////
          function getdistance(x, y, x1, y1) {                                        //and so as this one
          var run, rise;
          run = x1-x;
          rise = y1-y;
          return (_root.hyp(run, rise));
      }
      function hyp(a, b) {
          return (Math.sqrt(a*a+b*b));
      }
      MovieClip.prototype.reset = function() {
          //specify the width and height of the movie
          width = 650;
          height = 488;
          //-------------------
          var dist, norm;
          this.x = this._x;
          this.y = this._y;
          this.speed = Math.random()*4+2;
          this.targx = Math.random()*width;
          this.targy = Math.random()*height;
          dist = _root.getdistance(this.x, this.y, this.targx, this.targy);
          norm = this.speed/dist;
          this.diffx = (this.targx-this.x)*norm;
          this.diffy = (this.targy-this.y)*norm;
      };
      MovieClip.prototype.move = function() {
          if (_root.getdistance(this.x, this.y, this.targx, this.targy)>this.speed) {
              this.x += this.diffx;
              this.y += this.diffy;
          } else {
              this.x = this.targx;
              this.y = this.targy;
              if (!this.t) {
                  this.t = getTimer();
              }
              if (getTimer()-this.t>1000) {
                  this.reset();
                  this.t = 0;
              }
          }
          this._x = this.x;
          this._y = this.y;
      };
      //////////////////////////////////////////////////////////////////////////////////////
      stop();