- Home
- Tutorials
- Flash
- Intermediate
- Balls chain with constraints, colision, mouse follow and the math needed

All together
Alejandro Quarto
Partner, Software Development/General Manager at Persuasive Games. In his role, Mr. Quarto is responsible for software development and testing, as well as studio management and strategy.
Prior to joining to Persuasive Games, Mr. Quarto started his career in interactive entertainment as a freelance programmer using different object-oriented programming tools specifically specializing in Macromedia Flash, Shockwave, and Java development platforms among others. In 2002 Mr. Quarto co-founded Platico Games! in Buenos Aires, Argentina. While at Platico Games Studio, Mr. Quarto produced a wide variety of video game projects including: advergames, educational games, and multiplayer games oriented to big communities.
These games have been developed for clients such as: Claxson Interactive Group, Budweiser, Mattel, Turbo Video Brazil, Canal 13 (TV) Argentina, and FM HIT in Chile.Â
Quarto is currently teaching about Flash development, in an institute located in Buenos Aires. In his role as instructor, he teach how to develop webs and games, for online content. Personal Blog: http://www.alejandroquarto.com
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.


Author/Admin)