Aim a pistol to mouse position
I want to make a "game", with a pistol that always aim to where your mouse is, and shoots when you press the mouse button.
I have the pistol, but I don't know how to rotate it to the rigtht position, and I'm only 13 years old so I don't know enough math...
I'd be happy if you can give me a code that can rotate the pistol to the right position, and even happyer if you can explain what does the code actually do so I'll understand.
Thanks =]
Gun that points at the cursor
http://spacegrape.com/flashkit/gunPointer
http://spacegrape.com/flashkit/gunPo...gunPointer.fla
>Be sure that the 'center' of the gun is the point you want it to rotate around
>Aim it toward the right in the drawing (that's just how this code expects it to be oriented)
>put this code on the gun:
OnClipEvent (enterFrame) {
this._rotation = 57*(Math.atan2((_parent._ymouse-this._y),(_parent._xmouse-this._x)));
}
57 is about 180/pi
atan2 calculates the angle (in radians, which is why the 57) between the horizon and the line from gun to mouse
I don't remeber why I chose atan2 over atan, but there is no mathemagical difference there to worry about