AQFollowMouse

Kind of class:class
Inherits from:none
Author:Arthur Clemens
Classpath:org.asapframework.util.actionqueue.AQFollowMouse
File last modified:Thursday, 12 October 2006, 22:17:25
ActionQueue method to let a movieclip follow the mouse, with location and time parameters for precise control.

Summary


Class methods
  • followMouse (inMC:MovieClip, inDuration:Number, inTimeDiv:Number, inLocDiv:Number, inOffset:Point, inCallbackObject:Object, inCallBackMethod:Object) : ActionQueuePerformData
    • Lets a movieclip follow mouse movements.

Class methods

followMouse

static function followMouse (
inMC:MovieClip, inDuration:Number, inTimeDiv:Number, inLocDiv:Number, inOffset:Point, inCallbackObject:Object, inCallBackMethod:Object) : ActionQueuePerformData

Lets a movieclip follow mouse movements. Control parameters:
  • a time variable for delay effect
  • location variable for multiply effect
  • offset variable for setting a fixed distance to the mouse
  • callback method variable to get location feedback
Parameters:
inMC :
movieclip to move
inDuration :
(optional) the number of seconds that the movieclip should move; default is 0 (infinite); use -1 for instant change
inTimeDiv :
(optional) the reaction speed to mouse changes. Use 1.0 to set the inMC at the mouse location without delay. Use anything between 1.0 and 0 for a slowed down effect; default is 1.0.
inLocDiv :
(optional) the translation factor of the mouse movements, relative to its parent's center point; an inLocDiv of 2 multiplies all x and y (difference) locations by 2. The visual effect works best if the parent has its (0,0) location at its center. The value should not be smaller than inTimeDiv (and is set automatically to the value of inTimeDiv if it is smaller); default (when nothing is set) is 1.0.
inOffset :
(optional) the number of pixels to offset the clip from the mouse, defined as a Point object
inCallbackObject:
(optional) object to return the calculated value to; if defined, the drawing is not performed by followMouse and should be done in the object's callback method inCallBackMethod
inCallBackMethod:
(optional) method name or function reference of function to which the calculated value should be returned; the variable that is passed to this object is a org.asapframework.util.types.Point
Returns:
  • A new ActionQueuePerformData object.
Example:
  • The most simple way to let a movieclip move infinitely to the mouse is:
    queue.addAction( AQFollowMouse.followMouse, follow_mc );
    But the effect is probably a bit flickering. A smoother effect can be reached by adding a 'smooth factor' by passing a value to inTimeDiv:
    queue.addAction( AQFollowMouse.followMouse, follow_mc, 0, 0.5 );
    Use an inTimeDiv value close to zero to get a really long lag.
    To move the movieclip at some distance from the mouse pointer, use an offset. The following code sets the movieclip to a value of (10, -10) from the mouse:
    queue.addAction( AQFollowMouse.followMouse, my_mc, 0, null, null, new Point(10, -10) );
    The following example lets a movieclip follow the mouse and passes its position to callback method "followMouseLoc". The positioning of the movieclip is done in the callback method.
    queue.addAction( AQFollowMouse.followMouse, my_mc, 0, .09, .26, null, this, "followMouseLoc");
    // ...
    function followMouseLoc (inPos:Point) {
        my_mc._x = (STAGE_CENTER.x - inPos.x) * 0.25;
        my_mc._y = (STAGE_CENTER.y - inPos.y) * 0.25;
    }
    To keep the MovieClip oriented to the mouse position, you can use this function:
    private function followMouseLoc (inPos:Point) : Void {
    
        var dx:Number = inPos.x - graphic_mc._x;
        var dy:Number = inPos.y - graphic_mc._y;
        var d:Point = new Point(dx, dy);
    
        var startRotation:Number = graphic_mc._rotation;
        var endRotation:Number = NumberUtils.angle(d.x, d.y);
        var changeRotation:Number = endRotation - startRotation;
    
        if ((360 - changeRotation) < changeRotation) {
            changeRotation = changeRotation - 360;
        }
        if (360 - Math.abs(changeRotation) < Math.abs(changeRotation)) {
            changeRotation = 360 - Math.abs(changeRotation);
        }
        // do not rotate if the rotation will not be visible:
        if (Math.abs(changeRotation) > .5 ) {
            graphic_mc._rotation = endRotation - ((1 - ROTATION_EASE_FACTOR) * changeRotation);
        }
    
        // position clip
        graphic_mc._x = inPos.x;
        graphic_mc._y = inPos.y;
    }
    Where ROTATION_EASE_FACTOR is 0 < n <= 1.