ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Circle Collisions
http://www.actionscript.org/resources/articles/146/1/Circle-Collisions/Page1.html
FlashJunkie
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org. 
By FlashJunkie
Published on September 9, 2005
 
Tutorial details:
Written by: Flashjunkie [email:flashjunkie@canada.com]
Difficulty Level: Intermediate
Requirements: Flash 4
Download FLA

Page 1 of 1
Tutorial details:
Written by: Flashjunkie [email:flashjunkie@canada.com]
Difficulty Level: Intermediate
Requirements: Flash 4
Download FLA
As you can probably guess this code has a number of applications. The first that comes to mind is a an asteroid field but don't let your imagination stop there. So let's have a look at it. There is a tiny bit of code on the main timeline. This will set how many circles will appear on the screen among other things.
[as]
Comment: ----------------------------------------------
Comment: variable definitions
Comment: ----------------------------------------------
Set Variable: "total_circles" = 5
Set Variable: "movie_width" = 550
Set Variable: "movie_height" = 400
Set Variable: "speed" = 3
[/as]
The rest of the code is inside the circle and there is only ONE circle. The rest of the circles are separate instances of the original.

The Concept
Each circle has been named 1 through 5... which is necessary.

After initialisation the circles check to see if they've collided with a wall, then they check to see if (after they move their alloted movement for this turn) if they will be in collision... then they move themselves. Since this code is replicated as many times as you put instances of the circle on the movieboard (except I have only set 5 colors) you can scale the circle, etc, and the code will still work.

Getting Started
After creating a blank movie you will need to set up the main timeline. One layer for actions and the other for the circle(s). Like so.


Now you need to create a circle within a movie clip called circle_art. This is the basis for everything else. In circle_art we will now define the possible colours. Place 5 keyframes in the first 5 frames of the movie, each with there own copy of the circle. Now change the colour of each instance. If you are using something else other than circles you could have a different objects in each frame for example, diffenent shaped asteroids.

Defining the Colour
Once back in the main timeline you will have to create another movie clip, this time called circle. This will be placed on the circle layer of your main timeline. The circle works using 3 frames. The layer art contains an instance of circle_art.

The actions in the first frame are used to define the colour of the circle. This is then used in the second frame to create circles with different colours. The variable 'n' used in the second frame corresponds to the name of the circle and, amoung other things, the frame number in the movie clip circle_art and thus the colour.
[as]
Comment: ----------------------------------------------
Comment: pick a color
Comment: ----------------------------------------------
Begin Tell Target ("color")
Go to and Stop (GetProperty("../",_name))
End Tell Target
[/as]

The Actionscript
The Actionscript
The actions in the second frame provide the driving force behind the circle collisions .FLA. I've placed comments along the way to help you understand what's happening.
[as]
Comment: ----------------------------------------------
Comment: start move
Comment: ----------------------------------------------
Set Variable: "my_radius" = (GetProperty ("", _width) / 2)
Comment: 'n' is always 'name' of circle.
Set Variable: "my_x" = GetProperty("",_x)
Set Variable: "my_y" = GetProperty("",_y)
Set Variable: "my_n" = GetProperty("",_name)
If (ymov="")
Comment: *********************************
Comment: ----------------------------------------------
Comment: first run through code,
Comment: this will initialize circle
Comment: ----------------------------------------------
Comment: *********************************
Comment: pick a distance:
Set Variable: "ymov" = Random(/:speed)+1
Set Variable: "xmov" = Random(/:speed)+1
Comment:
Comment: pick a direction:
Set Variable: "ysign" = random(2)-1
If (ysign=0)
Set Variable: "ysign" = 1
End If
Set Variable: "xsign" = random(2)-1
If (xsign=0)
Set Variable: "xsign" = 1
End If
Comment: adjust x and y movement
Set Variable: "xmov" = xmov*xsign
Set Variable: "ymov" = ymov*ysign
Comment: ----------------------------------------------
End If
Comment: *********************************
Comment: ----------------------------------------------
Comment: check edges
Comment: ----------------------------------------------
If (my_x<=my_radius+/:speed)
Set Variable: "xmov" = xmov*-1
Set Variable: "my_x" = my_radius+/:speed
End If
If (my_y<=my_radius+/:speed)
Set Variable: "ymov" = ymov*-1
Set Variable: "my_y" = my_radius+/:speed
End If
If (my_x>=/:movie_width-my_radius-/:speed)
Set Variable: "xmov" = xmov*-1
Set Variable: "my_x" = /:movie_width-my_radius-/:speed
End If
If (my_y>=/:movie_height-my_radius-/:speed)
Set Variable: "ymov" = ymov*-1
Set Variable: "my_y" = /:movie_height-my_radius-/:speed
End If
Comment: ----------------------------------------------
Comment: check for collision
Comment: ----------------------------------------------
Set Variable: "n" = 1
Comment: start checking for collisions
Loop While (n<=/:total_circles)
//Comment: don't check myself
If (n ne my_n)
Set Variable: "n_x" = GetProperty("../"&n,_x)
Set Variable: "n_y" = GetProperty("../"&n,_y)
Set Variable: "n_radius" = (GetProperty ("../"&n, _width) / 2)
Set Variable: "delta_x" = (my_x+xmov)-(n_x)
Set Variable: "delta_y" = (my_y+ymov)-(n_y)
If ((delta_x * delta_x + delta_y * delta_y) < ((my_radius + n_radius) * (my_radius + n_radius)))
Comment: ----------------------------------------------
Comment: handle collisions
Comment: ----------------------------------------------
Comment: I collided with circle 'n'.
Set Variable: "nx" = Eval("../"&n&":xmov")
Set Variable: "ny" = Eval("../"&n&":ymov")
Comment: swap travel values with it.
Set Variable: "tempx" = xmov
Set Variable: "xmov" = nx
Set Variable: "../"&n&":xmov" = tempx
Set Variable: "tempy" = ymov
Set Variable: "ymov" = ny
Set Variable: "../"&n&":ymov" = tempy
Set Variable: "../"&n&":collision" = my_n
Else
End If
End If
Set Variable: "n" = n+1
End Loop
Comment: ----------------------------------------------
Comment: move me
Comment: ----------------------------------------------
Set Property ("", X Position) = my_x + xmov
Set Property ("", Y Position) = my_y + ymov
Comment: ----------------------------------------------
[/as]
Finally, the action in the third frame loops back to the second to keep the movie flowing and checking for collisions.
[as]
Go to and Play (_currentframe-1)
[/as]
It's that easy. It's a really simple concept with lots of potential, especially in games. I hope this has inspired you! Good luck and happy flashing.