Tutorial details:
Written by: Flashjunkie
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.
Comment: ----------------------------------------------
 Comment: variable definitions
 Comment: ----------------------------------------------
 Set Variable: "total_circles" = 5
 Set Variable: "movie_width" = 550
 Set Variable: "movie_height" = 400
 Set Variable: "speed" = 3

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.
Comment: ----------------------------------------------
 Comment: pick a color
 Comment: ----------------------------------------------
 Begin Tell Target ("color")
 Go to and Stop (GetProperty("../",_name))
 End Tell Target