1.4.15

Point at Pointer 3

Here is another look at the code:

addEventListener(Event.ENTER_FRAME, rotateTriangle, false, 0, true);

function rotateTriangle(e:Event) {
    var angle:Number = Math.atan2(mouseY - triangle.y, mouseX - triangle.x);
    triangle.rotation = angle * (180 / Math.PI);

}

If you notice, every time the frame changes, the Event Listener calls a function named "rotateTriangle". In that function, there is another function. It is the mathematical function known as Math.atan2 (). What does it do? Taking the distance between the x and y coordinates of two points, it triangulates the angle of a line from the second point to the first point.

In our case, the second point is the center of our triangle. The first point is our mouse pointer. Enter this code. Make sure that the names highlighted in blue match the name of the triangle instance that we created earlier.