1 Reply Latest reply on Apr 20, 2009 7:11 PM by FONGHO

    Constant speed movement of object

      Hi,

      I am trying to get an object to track my mouse on press and i am using a tween. However, the nature of the tween is such that the time taken for the object would move would always be constant despite the distance.

       

      I would like to make the object travel at a constant speed regardless of the distance. Is it correct for me to use a tween? If not what should I be using?

      The code for my tween is as follows.

       

      map_mc.tracker_mc._x = map_mc._xmouse;
      map_mc.tracker_mc._y = map_mc._ymouse;
      startx = map_mc.crosshair_mc._x;
      endx = map_mc.tracker_mc._x;
      starty = map_mc.crosshair_mc._y;
      endy = map_mc.tracker_mc._y;

       

      trackTweenx = new Tween(map_mc.crosshair_mc, "_x", Regular.easeIn, startx, endx, spd, true);
      trackTweeny = new Tween(map_mc.crosshair_mc, "_y", Regular.easeIn, starty, endy, spd, true);

       

      map_mc.tracker_mc is the mouse cursor that would appear when onPress while the map_mc.crosshair_mc is the object that would track my cursor's movements.

       

      Thanks.

        • 1. Re: Constant speed movement of object
          kglad Adobe Community Professional & MVP

          spd = distance/time

           

          because you want speed constant, calculate your tween distance:

           

          dist=Math.sqrt((startx-endx)*(startx-endx)+(starty-endy*(starty-endy));

           

          and use the following time in your tween:

           

          time = dist/spd;

           

          trackTweenx = new Tween(map_mc.crosshair_mc, "_x", Regular.easeIn, startx, endx, time, true);

           

          trackTweeny = new Tween(map_mc.crosshair_mc, "_y", Regular.easeIn, starty, endy, time, true);