A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Aim a pistol to mouse position

Hybrid View

  1. #1
    Junior Member
    Join Date
    Dec 2008
    Posts
    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. #2
    Senior Member ironmallet's Avatar
    Join Date
    Feb 2001
    Posts
    252

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

  3. #3
    GameWolfProductions
    Join Date
    Nov 2008
    Posts
    19
    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. #4
    Junior Member
    Join Date
    Nov 2008
    Posts
    25
    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