3 Replies Latest reply on May 23, 2012 12:32 PM by ImperialUser

    Move Object to clicked location

    sachin_mak

      hello,

       

      i tried below code,

       

      --------------------------------------------------------------------

      moveBall = function () {

      this.dx = (this.targx-this._x)*.1;

      this.dy = (this.targy-this._y)*.1;

      this._x += this.dx;

      this._y += this.dy;

      };

       

       

      setTarget = function () {

      this.targx = _root._xmouse;

      this.targy = _root._ymouse;

       

       

      };

      man.targx = 10;

      man.targy = 10;

      man.onMouseDown = setTarget;

      man.onMouseUp = moveBall;

      ---------------------------------------------------------------------

       

      it is working...but the problem is...my object moves just fewer steps not to the clicked location..

       

      can anyone help me???

        • 1. Re: Move Object to clicked location
          ImperialUser Level 1

          you are moving the ball 1/10 of the distance, once.  You need to stick a while loop in the moveBall function.

           

          Since you are doing it 10 times, easiest way to do it would be...

           

          ---------------------------------------------------------------------

          moveBall = function () {

          this.dx = (this.targx-this._x)*.1;

          this.dy = (this.targy-this._y)*.1;

           

          counter = 0;

          while(counter<10)

          {


          this._x += this.dx;

          this._y += this.dy;

           

          counter++;

          }


          };

          ---------------------------------------------------------------------

          • 2. Re: Move Object to clicked location
            sachin_mak Level 1

            thank you...

            it is working..

             

            but i like to create a man who goes to the clicked location by walking...but your code doesn't help me in that..

             

            becoz of your code object reaches in no second at clicked location.

             

            is there any solution of this??

            • 3. Re: Move Object to clicked location
              ImperialUser Level 1

              I'm still new to flash myself so this may not be correct but it's how I learned to do it.  It's hard to suggest without seeing more code, but I can talk it out.

               

              Using your original code, take out the while loop.  Move

              this.dx = (this.targx-this._x)*.1;

              this.dy = (this.targy-this._y)*.1;

              from moveBall to setTarget.

               

              Create a timer and boolean, on mouse down set the boolean to true.  Then in your timer

              if(boolean)

              { - do timer -

              if(timer == movement time)

              { - do movement -

                if(object == end location)

                {boolean = false}

              }

              }

               

              Hope that helps.