You'd think this would be easier - you'd think there would be a class already in existence for this. But as there isn't here's an ActionScript 2 solution.
The Script With Comments
The comments explain how it works:
/*sets starting points for the maths that comes later*/
var objectnewdeg = 0;
var objectdeg = 0;
/* This is the Rotate Object function, it updates the rotation of the object continuously as the user drags */
rotateobject = function () {
curpoint = (Math.atan2(_root._ymouse-_root.object_mc._y, _root._xmouse-_root.object_mc._x)/Math.PI)*180;
objectnewdeg = objectdeg+(curpoint-objectstartdeg);
_root.object_mc._rotation = objectnewdeg;
};
/* This initiates the Rotate Object function when the user clicks on the object */
_root.object_mc.onPress = function() {
/* This finds the position around the object that is first clicked in degrees and sets it as a variable that is used in the function above */
objectstartdeg = (Math.atan2(_root._ymouse-_root.object_mc._y, _root._xmouse-_root.object_mc._x)/Math.PI)*180;
/* starts the function above and sets the repeat frequency as 10 */
objectrotInterval = setInterval(rotateobject, 10);
};
/* This stops the Rotate Object function when the user releases the object, so it stays where the user left it, then records the new rotation of the object as objectdeg ready for when it is rotated again. */
_root.object_mc.onRelease = function() {
/* stops the function above */
clearInterval(objectrotInterval);
/*records the new rotation of the object as set by the user's drag as variable objectdeg, ready for next time they drag */
objectdeg = objectnewdeg;
};
/* This is almost a repeat of the release code above, but takes account of the fact the user might drift off the object when they drag it, hence onReleaseOutside. */
_root.object_mc.onReleaseOutside = function() {
/* stops the function above */
clearInterval(objectrotInterval);
/* records the new rotation of the object as set by the user's drag as variable objectdeg, ready for next time they drag */
objectdeg = objectnewdeg;
};The Script Without Comments
And if that code above looks scary, it's not so bad without comments - only 19 lines.
var objectnewdeg = 0;
var objectdeg = 0;
rotateobject = function () {
curpoint = (Math.atan2(_root._ymouse-_root.object_mc._y, _root._xmouse-_root.object_mc._x)/Math.PI)*180;
objectnewdeg = objectdeg+(curpoint-objectstartdeg);
_root.object_mc._rotation = objectnewdeg;
};
_root.object_mc.onPress = function() {
objectstartdeg = (Math.atan2(_root._ymouse-_root.object_mc._y, _root._xmouse-_root.object_mc._x)/Math.PI)*180;
objectrotInterval = setInterval(rotateobject, 10);
};
_root.object_mc.onRelease = function() {
clearInterval(objectrotInterval);
objectdeg = objectnewdeg;
};
_root.object_mc.onReleaseOutside = function() {
clearInterval(objectrotInterval);
objectdeg = objectnewdeg;
};Copy and paste the code into the first frame. Then place a MovieClip on the stage and give it the instance name 'object_mc'.
Test the movie and you should be able to click the object anywhere and drag it to rotate it.
The centre of rotation is the Registration point that you set when you made the object into a MovieClip.
If you want to re-use the code yourself, just replace every instance of the name object_mc with the instance name of your object.
Try It
Applying it to something useful
I originally developed this for use with my VirtualCompass teaching aid. This uses drag rotation to allow the user to position several different elements on the screen.