Starfield Simulation | Macromedia Flash
Traveling through space. Dots are moving along perspective linear paths. There are two MovieClip methods, randomSpace gives a movieclip a random xyz position in space. These would be the 3D coordinates of the stars in the real world. The placeInSpace method calculates the actual screen coordinates of the movieclip and also sets the positions in _x and _y. This method also scales the size of the movieclip by setting _xscale and _yscale. The closer the stars are the bigger they are indeed.
MovieClip.prototype.placeInSpace = function(id) {
var ratio = this.focaldistance / Math.sqrt(this[id].z * this[id].z);
this[id]._x = this[id].x * ratio;
this[id]._y = this[id].y * ratio;
this[id]._xscale = this[id]._yscale = 1000 / this[id].z;
}
MovieClip.prototype.randomPlace = function(id) {
var radius = this.rmin + (this.rmax - this.rmin) * Math.random();
var polar = 2 * Math.PI * Math.random();
this[id].x = radius * Math.cos(polar);
this[id].y = radius * Math.sin(polar);
this[id].z = this.zmin + (this.zmax - this.zmin) * Math.random();
}
Having these two methods we can create a host clip that will call these methods on every movieclip. On startup the code block executes inside the onClipEvent(load) handler. It populates the stage with the "dot" library item. One could also use bitmaps as sprites. Finally as the onClipEvent (enterFrame) handler exectues on every frame, it calls placeInSpace to present them on the screen. When a movieclip has passed the limit of onscreen display we recycle it with the randomPlace method.
onClipEvent (load) {
this.count = 50;
this.speed = 5;
this.focaldistance = 25;
this.rmin = 50;
this.rmax = 1000;
this.zmin = 10;
this.zmax = 300;
for(var i = 0; i < this.count; i++) this.attachMovie("dot", i, i);
}
onClipEvent (enterFrame) {
for(var i = 0; i < this.count; i++) {
if((this[i].z -= this.speed) <= 0) this.randomPlace(i);
this.placeInSpace(i);
}
}
Reflections from Guests (in orange):
Damn... This sample remember me the good ol'time of Assembler :D Easier than Assembler for reading but more complicated since it's fully virtual....
very nice. now is there a way to control the speed and direction during movie? that would be awesome.
You can change the speed where it reads this.speed = 5; within the field movieclip. Changing directions? Not at the moment, but a definitely good idea.
Love this effect -- I'm assuming that by 'changing directions' above you mean reverse the anmation so that the stars are all shrinking to the same point. I wish there was a way to do this! Am I correct to assume that I understood and that there is no way to do this?
There are ways to do this. You have to change the randomPlace method and the condition within onClipEvent (enterFrame). If bugged enough I am inclined to improve this script.
GAAAAA It's the simplest one i've seen! But, IT'S STILL TOO COMPLICATED FOR ME!!! AHHHHH ACTION SCRIPT AHH!!!!
haha, you sound like me :)
Sorry, I do not have time to answer questions related to these scripts.
You can always get help on the Kirupa Flash Forums. Thank you for visiting.

