Finally, we are going to put all together, we just need to create some instructions for our functions usage.

The last code:

this.onEnterFrame = function() { followMouse(mc1,0); for (i=1; i<=balls; i++) { a = i+1; follow(_root["mc"+a],_root["mc"+i],(_root["mc"+a]._width/2)+(_root["mc"+i]._width/2)); for (j=1; j<=balls; j++) { if (j<>i) { keepDistance(_root["mc"+j],_root["mc"+i],(_root["mc"+i]._width/2)+(_root["mc"+j]._width/2)); } } } };



I've used the enterFrame, anyway you can build a function and use the setInterval to avoid FPS problems, and fine tuning.
We need to update our system constantly. First of all, our ball number 1 should follow the mouse with our followMouse function, first argument is our ball, second is the distance...in this case just 0.
Then, there is a Loop, main idea behind this is that we need each ball to follow the next one, in this way we have the chain. With our function follow we send 3 arguments: 1. ball to move, 2: ball to follow, 3. minimum distance between both, in this case the sum of both radius.
Finally, we have another loop, a nested one. Why this? Well, if we don't check each ball with all the others, is hard to see how all the balls overlaps. That's why we made a "keepDistance" function. This means, for each ball, we check distances for all the balls (not the same one). If there is an overlap, then move it.
That's all!
I hope you can enjoy this tutorial, and you can always get an explanation in spanish in my personal blog: www.alejandroquarto.com
Sorry about my english.
Source code in Flash 8 in the attachment at the bottom of the page.