code on 2008-12-21
package {
import flash.display.*;
import flash.events.*;
import flash.filters.*;
[SWF(width="465", height="465", backgroundColor="0x000000", frameRate="15")]
public class CheapStarField extends Sprite{
private var starContainer:Sprite;
private var starLimit:int = 100;
public function CheapStarField(){
init();
}
private function init():void{
starContainer = new Sprite();
addChild(starContainer);
starContainer.x = stage.stageWidth * .5;
starContainer.y = stage.stageHeight * .5;
starContainer.filters = [new BlurFilter(2,2,2)];
for(var i:int = 0 ; i < starLimit; i++){
addStar();
}
addEventListener(Event.ENTER_FRAME, update);
}
private function addStar():void{
var star:MovieClip = new MovieClip();
star.graphics.beginFill(0xFFFFFF);
star.graphics.drawCircle(0,0,1 + Math.random()*5);
star.graphics.endFill();
star.rotation = Math.random() * 360;
star.vx = (Math.cos(star.rotation / 180 * Math.PI) * 20);
star.vy = (Math.sin(star.rotation / 180 * Math.PI) * 20);
starContainer.addChild(star);
}
private function update(e:Event):void{
for(var i:int = 0; i < starContainer.numChildren ; i++){
var star:MovieClip = MovieClip(starContainer.getChildAt(i));
star.x += star.vx;
star.y += star.vy;
if(star.x < - stage.stageWidth * .5 || star.x > stage.stageWidth || star.y < -stage.stageHeight * .5 || star.y > stage.stageHeight){
starContainer.removeChild(star);
addStar();
}
}
}
}
}