Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 3 of 3

Thread: lining up movieclip with mouse cursor

  1. #1

    lining up movieclip with mouse cursor

    How would you create the effect in this game: http://www.newgrounds.com/portal/view/375622
    where the hero's gun/arms align up with the mouse cursor?

    I'm guessing there is trigonometry involved and tracking _xmouse/_ymouse, but I'm not to sure. Any help is appreciated.

    Here's what I have so far. I think the problem is in my math.
    Any ideas.

    Code:
    onEnterFrame = function ():Void {
        addScope();
        var armY = _ymouse-this.hero_mc.arm_mc._y;
        var armX = _xmouse-this.hero_mc.arm_mc._x;
        var radians = Math.atan2(armY, armX);
        this.hero_mc.arm_mc._rotation = radians*90/Math.PI;
    };
    
    function addScope():Void {
        Mouse.hide();
        scope_mc.onMouseMove = function() {
            this._x = _root._xmouse;
            this._y = _root._ymouse;
            // updateAfterEvent used to refresh stage creating smooth cursor movement
            updateAfterEvent();
        };
    }

  2. #2
    I didn't really look your code over too well but it looks like you have the right idea. I remember after about an hour of playing around I created this lump of code to get a movieclip to point at the mouse. Hope it helps.


    Code:
    xTanDist = _xmouse - Arrow._x;
    yTanDist = _ymouse - Arrow._y;
    
    Radians = Math.atan2(yTanDist, xTanDist)*10
    	
    Rotation = Math.round(5.8*Radians)
    Arrow._rotation = Rotation+90
    Attached Files Attached Files


  3. #3
    207
    posts
    Registered User
    The first thing to jump out at me is you're using *90/Math.PI; where I usually would have *180/Math.PI;

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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 About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012