A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Aim a pistol to mouse position

  1. #1
    Junior Member
    Join Date
    Dec 2008

    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. #2
    Senior Member ironmallet's Avatar
    Join Date
    Feb 2001

    Gun that points at the cursor


    >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
    Last edited by ironmallet; 12-24-2008 at 01:35 AM. Reason: fix link

  3. #3
    Join Date
    Nov 2008
    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) {
    onClipEvent (enterFrame) {
    k=Math.atan (e);
    onClipEvent (enterFrame) {
    if (_root._ymouse>_parent._y) {
    } else if (_root._ymouse<_parent._y) {

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

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

  4. #4
    Junior Member
    Join Date
    Nov 2008
    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
    Attached Files Attached Files

  5. #5

Posting Permissions

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

Click Here to Expand Forum to Full Width

HTML5 Development Center