A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: mc following mouse but within limits and smoothly

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    34

    mc following mouse but within limits and smoothly

    Hi guys,

    I am doing an animation with a movie clip that needs to follow the mouse on the Y axis only smoothly and needs to stay inside a fixed dimension. (e.g. the mc could moves 200px left or 200px right maximum) And I want it to stop smoothly too when it reach the limits. Any help would be appreciate, even if you don't know how to do it all, any code that can help reach the solution would be really appreciated.

    Thanks guys

  2. #2
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352
    your basically using the mouseY property.

    you can set an event for ROLL_OVER (MOUSE_OVER, OVER, whatever, i cant remember). then it activates a function to modify position on frame. something like this:

    // put this code inside the movieclip, otherwise append the mc ID before the listeners. if this event applies to the whole stage, then just keep the ENTER_FRAME event, and remove the rest.

    // this example assumes the code is on the main timeline.

    Code:
    addEventListener(MouseEvent.ROLL_OVER, onOver);
    addEventListener(MouseEvent.ROLL_OUT onOut);
    
    function onOver(e:MouseEvent):void {
         addEventListener(Event.ENTER_FRAME, onFrame);
    }
    
    function onOut(e:MouseEvent):void {
         removeEventListener(Event.ENTER_FRAME, onFrame);
    }
    
    function onFrame(e:Event):void {
         if(movieClip.y < (#y position for maximum && movieClip.y > (#y position for minimum) {
              // sort the tweening out yourself, your looking for the 'easing' property.
              // use the Tween class or download TweenLite.
              moveClip.y = stage.mouseY;
         }
    }
    flos

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    34
    Hi guys, thanks again for the help. I found a way to make the script as I wanted but I am still wondering if I could do something more to ease the end of the animation almost like it was still going on for 1 sec or half, after the mouse has stopped. Here's the script I will be using;

    addEventListener(Event.ENTER_FRAME, loopX);


    function loopX(e:Event):void
    {
    var xDist:Number = mouseX - 24000

    front_mc.x = xDist * -0.02;

    }

    As you can see, I made a mistake in my tread, it needs to move on the X axis.
    Thanks

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