type = 'application/x-shockwave-flash'
data = 'http://sitepointstatic.com/flash/starfield.swf'
width = '400'
height = '400'
bgcolor = 'black'
>

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!

Georgina has more than fifteen years' experience writing and editing for web, print and voice. With a background in marketing and a passion for words, the time Georgina spent with companies like Sausage Software and sitepoint.com cemented her lasting interest in the media, persuasion, and communications culture.

Get your free chapter of Level Up Your Web Apps with Go

Get a free chapter of Level Up Your Web Apps with Go, plus updates and exclusive offers from SitePoint.


No Reader comments

Related books & courses
Available now on SitePoint Premium

Preview for $1