ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Tutorial on Creating Following Objects.
http://www.actionscript.org/resources/articles/659/1/Tutorial-on-Creating-Following-Objects/Page1.html
Pratik. Velani
 
By Pratik. Velani
Published on December 31, 1969
 

Lesson 1 : Following Mouse W/O Ease.
    Creating a movie clip :
    Create new flash document.


    Draw a circle using the oval tool.


   

    Select the circle using double click. Press F8 to convert to symbol.


    Part 1 :
   
       Click on the circle and Press F9. Actionscript Panel will open. Insert the following actionscript.

[as]
onClipEvent(enterFrame){
    diffx = _root._xmouse - this._x ;
    diffy = _root._ymouse - this._y ;
    _x += diffx ;
    _y += diffy ;
}
[/as]


    Explanation about the code

_xmouse :   returns the x coordinate of the mouse.
_ymouse :  
returns the y coordinate of the mouse.
_x            :  
An integer that sets / stores the x coordinate of an object.
_y            :  
An integer that sets / stores the y coordinate of an object.

    variable diffx & diffy is used to store the distance between mouse's x & cordinate and object's x & y cordinate respectively.
     _x += diffx ;
    Assigns _x , the value of _x + diffx .
    _y += diffy ;
    Assigns _y , the value of _y + diffy .



    Part 2 :

replace

[as]
_x += diffx ;
_y += diffy ;
[/as]

with

[as]
_x += diffx / 5 ;
_y += diffy / 5 ;
[/as]

Explanation of code :

_x += diffx / 5 ;
    Assign _x , the value of _x + diffx / 5 ;( dividing the diffrence with a number decreases the speed of movment. Higher the number lower the speed. )
_y += diffy / 5 ;
    Assign _y , the value of _y + diffy / 5 ;( dividing the diffrence with a number decreases the speed of movment. Higher the number lower the speed. )



Lesson 2 : Following with a trail effect
    Create a movie clip ( you can refer to step 1 on page 1 of this tutorial ).
Click on circle and press F9 , to open actionscript panel , insert the following code :

[as]
onClipEvent(load){
    _root.createEmptyMovieClip("line_mc" , -19000) ;
    _root.line_mc.lineStyle(5, 0x0066CC , 100) ;
    _root.line_mc.moveTo(this._x , this._y) ;
}
onClipEvent(enterFrame){
    diffx = _root._xmouse - this._x ;
    diffy = _root._ymouse - this._y ;
    _x += diffx / 5 ;
    _y += diffy / 5 ;
    _root.line_mc.lineTo(this._x , this._y) ;

}
onClipEvent(mouseDown){
    _root.line_mc.clear() ;
    _root.line_mc.linestyle(5, 0x0066CC , 100) ;
    _root.line_mc.moveTo(this._x , this._y) ;
}
[/as]

    Explanation  of code :

_root.createEmptyMovieClip("line_mc", -19000) ;
      
Creates an empty movie clip at the root level , with the instance name line_mc at -19000 depth .

_root.line_mc.linestyle(5, 0x0066CC , 100) ;
       Assigns a linestyle to movie clip line_mc , with the following parameters.
       Thickness - 5 ;
       RBG          - 0x0066CC ;
       Alpha        - 100 ;

 _root.line_mc.moveTo(this._x , this._y) ;
      
Moves the current drawing position to ball_mc's x & y coordinates .

_root.line_mc.lineTo(this._x , this._y) ;
      
Draws a line using the linestyle assigned , from the current drawing position to the coordinates mentioned .

_root.line_mc.clear() ;
      
Removes all the graphics created during runtime by using the movie clip draw methods , including the linestyles specified .


Lesson 3 : Following other objects .
    Create two movie clips ( refer to step 1 on page for creating movie clips ).

Change the instance name of first movie to ball_mc and instance of second movie to ball2_mc .
Click on the first movie clip and press F9 and insert the following code;

Code on first movie clip
[as]
onClipEvent(enterFrame){
    diffx = _root._xmouse - this._x ;
    diffy = _root._ymouse - this._y ;
    _x += diffx/5 ;
    _y += diffy/5 ;
}
[/as]

Code on second movie clip
[as]
onClipEvent(enterFrame){
    diffx = _root.ball_mc._x - this._x ;
    diffy = _root.ball_mc._y - this._y ;
    _x += diffx/5 ;
    _y += diffy/5 ;
}
[/as]

Explanation of second movie clip actionscript code :
    diffx = _root.ball_mc._x - this._x ;

    diffy = _root.ball_mc._y - this._y ;
      
Assigns diffx & diffy , the diffrence of ball_mc's and ball2_mc's x & y coordinates respectively .



    Final :
       You can freely experiment and create ur own effects .....