Orient an object to the mouse cursor

How to orient an objet to an other ? Just put those lines and change the instance name myTank. Get Adobe Flash player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This object will always look at the mouse cursor
myTank.addEventListener(Event.ENTER_FRAME,  tankEnterFrame);
// This function will be launched every frame (25 times by seconds)
function tankEnterFrame(pEvt) {
    // pEvt.currentTarget : myTank
    var mc=pEvt.currentTarget;
    // Get the radian angle between the tank and the cursor
    // You can also replace mouseX and mouseY by another coordinates
    var angleRadian=Math.atan2(mouseY-mc.y,mouseX-mc.x);
    // Convert the radian angle in dedree
    var angleDegree=angleRadian*180/Math.PI;
    // Set the orientation
    mc.rotation=angleDegree;
    // Display angle of rotation in degree
    txtAngle.text=Math.round(angleDegree)+"°";
}

Download the source code here : [www.thisisgameplay.com]_Orient_object_to_mouse_cursor.fla

VN:F [1.9.3_1094]
Rating: 8.5/10 (17 votes cast)
VN:F [1.9.3_1094]
Rating: +3 (from 3 votes)
Orient an object to the mouse cursor, 8.5 out of 10 based on 17 ratings
  • Share/Bookmark
Posted on January 9, 2010 at 18:17 by admin · Permalink
  • Share/Bookmark

In: ActionScript, Tutorial · Tagged with: , ,

Leave a Reply