-- FOSS: Starfield --
In this topic, you'll find my dynamic starfield engine. As usual and for all FOSS topics, feel free to use this code as you wish, and you shall share your modifications of this engine (if you made some) on this topic.
What should be useful to know before using this engine (links redirects to the corresponding AS topic):
Beginning ActionScript, ClipEvents, Frame functions, Loops and conditions, Variables, Maths basics, Duplicated movie clips, Functions - Basics,
The code:
for (n=1; n<250; n++) {
newStar(n);
}
function newStar(i) {
_root.attachMovie("star_mc", "star"+i, i);
_root["star"+i]._x = random(640);
_root["star"+i]._y = random(480);
_root["star"+i]._alpha = 0;
_root["star"+i].xfactor = (_root["star"+i]._x-320)/20;
_root["star"+i].yfactor = (_root["star"+i]._y-240)/20;
_root["star"+i].alphafactor =int(100*(Math.abs(_root["star"+i].xfactor
)+Math.abs(_root["star"+i].yfactor)))/28;
_root["star"+i].onEnterFrame = function() {
if (this._alpha<this.alphafactor) {
this._alpha += 5
}
this._x += this.xfactor;
this._y += this.yfactor;
if (this._x<0 || this._x>640 || this._y<0 || this._y>480) {
newStar(251+i);
this.removeMovieClip();
}
};
}
Note: tabulations and line breaks were removed to prevent the NG forum from messing the code.
Usage:
Put this code on a frame (root timeline recommended, unless you know how to integrate correctly this code in a movie clip).
This code was created for a Stage size of 640X480, with black background and 25 FPS. For any customisations, post here.
Help and comments:
Feel free to ask customisations of this code to NG users or giving comments on this code here. Further help can be given via email, if it's really necessary (check my profile).
~gorman2001.