A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: help for following mouse

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    2

    help for following mouse

    Hi guys,

    I'm having issues with a couple of things:

    1) Replace the mouse with an image (that I didn't create inside Flash)
    2) Have another image follow the mouse

    I've looked up a number of tutorials about this, but they all give instructions based on having created the image inside Flash. I made my images in Photoshop, and I'm not sure why but when I try to paste in the actionscript for them, nothing works.

    Particuarly, I was looking at this thing: http://articles.sitepoint.com/articl...mouse-follower , I kind of like the rotation but it's not necessary!

    I'm more of an artist than a programmer, so I really don't even know where to begin in troubleshooting this. If someone could help me out on this, I'd appreciate it a bunch!

  2. #2
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    import the mouse replacing image into your libraby from the file menu. select it, press F8, make sure it's a movieclip and the registration point is set to center. name it mouse_mc.

    inside it, on on _root (whatever you prefer)
    write the code:

    Mouse._visible = false;
    onMouseMove = function(){
    mouse_mc._x = _xmouse;
    mouse_mc._y = _ymouse;
    }

    if you want something else following your mouse (follow_mc), there's more than one way, but a simple one would be this:

    var velocity:Number = 20;

    onEnterFrame = function(){

    var xdif:Number = _xmouse - follow_mc._x;
    var ydif:Number = _ymouse - follow_mc._y;

    var v:Number = Math.sqrt(xdif*xdif + ydif*ydif);
    follow_mc._x += xdif * velocity/v;
    follow_mc._y += ydif * velocity/v;

    }


    EDIT:

    this would be even better on the 2nd one...



    var velocity:Number = 20;

    onEnterFrame = function(){

    var xdif:Number = _xmouse - follow_mc._x;
    var ydif:Number = _ymouse - follow_mc._y;
    var v:Number= Math.sqrt(xdif*xdif + ydif*ydif);


    follow_mc._x += xdif * velocity/v;
    if(Math.abs(follow_mc._x - _xmouse) < velocity/2){
    follow_mc._x = _xmouse;
    }

    follow_mc._y += ydif * velocity/v;
    if(Math.abs(follow_mc._y - _ymouse) < velocity/2){
    follow_mc._y = _ymouse;
    }

    }
    Last edited by koenahn; 09-03-2010 at 04:22 PM.

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    2
    Ahh thanks so much! I had to alter the code a bit for the mouse (not sure why), but the following works great. Thanks again for your help!

  4. #4
    Senior Member
    Join Date
    Mar 2010
    Posts
    157
    no problem, glad to know it works wel

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