13 Replies Latest reply on Jun 16, 2007 7:07 AM by mortentwellmann

    _rotation value problem

    mortentwellmann
      I have tried to make a movieclip rotate in relation to the position of the mouse and this was solved by finding a script as follows:

      function rotateTowards (clip, targetX, targetY) {
      // Create a point object that stores the x and y coordinates of
      // the clip relative to its parent's registration point
      var clipPoint = {x:clip._x, y:clip._y};

      // Convert local clip coordinates to global (Stage) coordinates
      clip._parent.localToGlobal(clipPoint);

      // Measure the distance between the registration
      // point of the clip and the target point
      var deltaX = targetX - clipPoint.x;
      var deltaY = targetY - clipPoint.y;

      // Calculate the angle of the line from the registration point
      // of this clip to the mouse
      var rotationRadian = Math.atan2(deltaY, deltaX);

      // Convert the radian version of the angle to degrees
      var rotationAngle = radiansToDegrees(rotationRadian);

      // Update the rotation of this clip to point to the mouse
      clip._rotation = rotationAngle;
      }
      function radiansToDegrees(radians) {
      return (radians/Math.PI) * 180;
      }

      //The movieclip then has the following code attached:
      onClipEvent (enterFrame) {
      _root.rotateTowards(this, _root._xmouse, _root._ymouse);
      }

      This works very well, but I wanted to put a delay on the rotation, i.e. the rotation should follow slowly after having moved the mouse to give a more smooth feel.

      To do this I tried calculating the difference between the rotation and then dividing by 16 and assigning this number to the _rotation property and it works fine UNTIL: The value passes the gap from 180 to -180 degrees or -180 degrees. The movieclip suddendly spins the other way around (as it is apparently closer to spin counter-clockwise than clock-wise...)

      What is the workaround for this?

      - Morten