A Flash Developer Resource Site

Thread: Aim a pistol to mouse position

1. 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 =]

2. 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)));
}

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

3. 1. Make a movie clip of the character's head or whatnot to point at the mouse. Draw it facing right.

2. Make another movie clip that is completely transparent.

3. Put the movie clip from step 2 in the middle of the character's body/head and put this code into it:

onClipEvent (enterFrame) {
z=_root._xmouse-_parent._x;
r=_root._ymouse-_parent._y;
_root.a=z;
_root.b=r;
}
onClipEvent (enterFrame) {
e=z/r;
k=Math.atan (e);
}
onClipEvent (enterFrame) {
g=k*180/Math.PI;
_rotation=g;
if (_root._ymouse>_parent._y) {
_parent._rotation=90-_rotation;
} else if (_root._ymouse<_parent._y) {
_parent._rotation=180-(_rotation-90);
}
}

4. Move the complete character into the main screen. Ready to go!

Or the other guy's solution. That one's good too.

4. This is a simple file I made as a concept which I never bothered expanding. Go nuts.

Demo here: http://megaswf.com/view/85ef337d295f...ec6ed6482.html

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap