Simple maths in AS3 part 2 – rotation

Hello,

This time a short explanation how to use trigonometry and rotation to achieve a ‘point_to’ effect. The maths is really simple here and useful for various effects and other uses. I’ve used a mousemove listener on the stage to execute the function and mouseX + mouseY to give the coordinates to the script. The movieclip (or sprite if you wish) can be rotated to face the direction of a point you specify (in this case – the mouse position on the stage).

Here is the code (as3):

var dx,dy,target_rotation:Number;

function point_to(what)
{
dx = mouseX – what.x;
dy = mouseY – what.y;
what.rotation = Math.atan2(dy, dx) * 180 / Math.PI;
}

// example usage
stage.addEventListener(MouseEvent.MOUSE_MOVE,stage_MOUSEMOVE);
function stage_MOUSEMOVE(e:MouseEvent):void
{
 point_to(movie_mc);
}

You can download the FLA file here. The example can be seen here.

Feel free to comment and have fun with Flash!,

FLAIM

3 Responses to “Simple maths in AS3 part 2 – rotation”

  1. Tim Says:

    Thanks, this was very helpful. Modified to fit my specifications with ease.

  2. Porter Says:

    Thanks for the tutorial. I always forget small pieces of code related to rotation and trig and this was exactly what I needed. Nicely organized and well working, keep it up.

  3. brent Says:

    Nice article thanks for sharing!


Leave a Reply