Flash Script – Star Field Effect

Georgina Laidlaw
Georgina Laidlaw

Creating a starfield effect is easy! To begin, download the sample files here.

1. Create a small star shape, and convert it to a movie clip. Give it an instance name of star.

2. Extend the existing layer by three frames. Then, create a new layer above the existing layer, and create three blank frames in this layer.

3. In the first layer, insert the actions:

numstars = 100; 
speed = 5;
star_field = new Array (numstars);
//x is x axis, y is y axis, speed is speed of star
function star(x,y,z)
 this.x = x;
 this.y = y;
 this.speed = speed;
for (i=0;i<numstars;i++)
star_field[i] = new star(random(300),random(200),random(speed)+1);
duplicateMovieClip ("star", "star_" add i, i+10);

4. In the second key frame, insert this action:

for (i=0;i<numstars;i++) 
star_field[i].x+= star_field[i].speed;
if (star_field[i].x>300) { star_field[i].x = 0; }
setProperty ("star_" add i , _x , star_field[i].x )
setProperty ("star_" add i , _y , star_field[i].y )
setProperty ("star_" add i , _alpha , star_field[i].speed*40 )

5. Finally, in the third, insert:

gotoAndPlay (2);

That’s it! Test the movie!