Starfield
This code uses the following:
Intervals, Functions, attachMovie, Variables, Maths, Conditions, Movement, Arrays, and a touch of API.
====================== ======================
This is a simple star field, created dynamically. Add this code to the first frame of your movie:
//Black background, 20+ FPS
var frequency=50; //new stars per second
var speed=5; //star speed
var accel=20; //star acceleration
SW=Stage.width; SH=Stage.height;
c=1000;
//Create timer function
interv = setInterval(function () {
makestar()
}, 1000/frequency);
//Function to create a new star
function makestar(){
c++; //Use for unique Instance Name and depth
if(c>2000){ c=1000; }
//MAKE NEW STAR
_root.createEmptyMovieClip("star"+c, c)
with(_root["star"+c]){
//Draw a circle (with API)
lineStyle(2, 0xFFFFFF, 100);
lineTo(0.2, 0);
//Set position, direction and alpha
_x=SW/2; _y=SH/2;
_alpha=0;
_rotation=random(360);
}
//Set speed
_root["star"+c].spd=random(speed/2)+speed/
2;
//TELL NEW STAR WHAT TO DO EACH FRAME
_root["star"+c].onEnterFrame= function(){
with(this){
//Speed it up, make it bigger
spd*=1+(_root.accel/500);
_width*= 1+(_root.accel/2500);
_height*= 1+(_root.accel/2500);
_alpha+=3;
//Move it
if(_rotation>180){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
//If off-screen, remove star
if(_x<0-_width || _x>SW+_width || _y>SH+_width ||_y<-_width){
this.removeMovieClip();
}
}
}
}
====================== ======================
This is virtually the same code, but uses an attached movie rather than an API dot as the star. For this to work, you'll need to:
Right-click the MC in the library.
Select 'Linkage'
Tick the 'Export for Actionscript' box
Give it the Identifier 'star'
Add this code to the first frame of your movie:
//Black background, 20+ FPS
var frequency=50; //new stars per second
var speed=5; //star speed
var accel=20; //star acceleration
SW=Stage.width; SH=Stage.height;
c=1000;
//Create timer function
interv = setInterval(function () {
makestar()
}, 1000/frequency);
//Function to create a new star
function makestar(){
c++; //Use for unique Instance Name and depth
if(c>2000){ c=1000; }
//MAKE NEW STAR
_root.attachMovie("star", "star"+c, c)
with(_root["star"+c]){
//Set position, direction and alpha
_x=SW/2; _y=SH/2;
_alpha=0;
_rotation=random(360);
}
//Set speed
_root["star"+c].spd=random(speed/2)+speed/
2;
//TELL NEW STAR WHAT TO DO EACH FRAME
_root["star"+c].onEnterFrame= function(){
with(this){
//Speed it up, make it bigger
spd*=1+(_root.accel/500);
_width*= 1+(_root.accel/2500);
_height*= 1+(_root.accel/2500);
_alpha+=3;
//Move it
if(_rotation>180){
_y+=(spd*Math.cos(Math.PI/180*_rotation));
_x-=(spd*Math.sin(Math.PI/180*_rotation));
}else{
_y-=(spd*Math.cos(Math.PI/180*_rotation));
_x+=(spd*Math.sin(Math.PI/180*_rotation));
}
//If off-screen, remove star
if(_x<0-_width || _x>SW+_width || _y>SH+_width ||_y<-_width){
this.removeMovieClip();
}
}
}
}
RESULT (MC was a light blue line)
====================== ======================
WHAT!?
Both of these codes basically do the same thing: they create a new star at the centre of the stage every X milliseconds, based upon the interval set.
As it is initiated, each star is assigned position, speed, alpha and rotation values.
Then, on each new frame, the star's values for alpha, size and speed are adjusted. The star is then moved based upon these settings.
Finally, when the star is out of sight, it is removed.
====================== ======================
Some interesting variations:
To spin yourself out, find this line in the code:
_alpha+=3;
And add this after it:
_rotation+=5;
RESULT
To make the 'source spot' follow the mouse, replace this line:
_x=SW/2; _y=SH/2;
with these:
_x= _root._xmouse;
_y= _root._ymouse
This looks better if you change this line:
_alpha=0;
to
_alpha=50;
RESULT
For multicoloured stars, use the first (API dot) code, and add this at the top:
cols=new Array(0xFF0000, 0xFF9900, 0xFFFF00, 0x00FF00, 0x00FFFF, 0x0000FF, 0xFF66FF, 0xCC33FF)
Then change this line:
lineStyle(2, 0xFFFFFF, 100);
to this:
lineStyle(2, cols[random(cols.length)], 100);
RESULT
====================== ======================
Basically, play with bits of the code to see what happens when you adjust values. This is quite a good sample for trying out various different aspects of AS. Have fun =)