/********************************************
* blockfield effekt
* generats a starfield of blocks
********************************************/
onClipEvent(load){
// initvars
n = 80; //blocks
maxdist = 800; //startdistance
speed = 5; //blockspeed
height=300; //viewport height
width=600; //viewport width
startfieldwidth=width*maxdist/2; //box starting retangle height
startfieldheight=height*maxdist/2; //box starting retangle width
scalefac = 0.2; //max box scaling
delay = 100; //starting delay in frames
xstart = width/2;
ystart = height/2;
/////////////////////////////////////
// initalize box clip after ataching
/////////////////////////////////////
function initbox(clip){
x=Math.random()*startfieldwidth - startfieldwidth/2;
y=Math.random()*startfieldheight - startfieldheight/2;
z=Math.random()*maxdist;
_root[clip].delay = Math.random()*delay;
_root[clip].x=x;
_root[clip].y=y;
_root[clip].z=200;
_root[clip].swapDepths((maxdist-_root[clip].z)*10+1000+math.random()*10);
rotate(clip,10);
updatebox(clip);
}
/////////////////////////////////////
// rotate box clip
/////////////////////////////////////
function rotate(clip,deg){
_root[clip].box.gotoAndStop(math.floor(deg/10));
}
/////////////////////////////////////////////////////
// initalize boxfield: create box clip and init it
////////////////////////////////////////////////////
function initstarfield(){
for(i=1;i<n;i++){
_root.attachMovie("bb", "box"+i, i);
initbox("box"+i);
}
}
///////////////////////////////////////////////////
// update boxfield (will be called every frame
///////////////////////////////////////////////////
function updatestarfield(){
for(i=1;i<n;i++){
_root["box"+i].z-=speed;
updatebox("box"+i);
}
}
///////////////////////////////////////////////////
// reset box if its out of bounds
///////////////////////////////////////////////////
function resetbox(clip){
x=Math.random()*startfieldwidth - startfieldwidth/2;
y=Math.random()*startfieldheight - startfieldheight/2;
z=maxdist
x0 = x/z +xstart;
y0 = y/z +ystart;
_root[clip].x=x;
_root[clip].y=y;
_root[clip].z=z;
_root[clip]._x=x0;
_root[clip]._y=y0;
rotate(clip,0);
_root[clip].swapDepths((maxdist-_root[clip].z)*10+1000+math.random()*10);
}
///////////////////////////////////////////////////
// update visual appearance
///////////////////////////////////////////////////
function updatebox(clip){
if(_root[clip].delay<=0) {
_root[clip]._visible = true;
if(_root[clip].z<0) resetbox(clip) ;
if(_root[clip]._y<=0||_root[clip]._y>=height) resetbox(clip);
if(_root[clip]._x<=0||_root[clip]._x>=width) resetbox(clip);
x0 = _root[clip].x/_root[clip].z +xstart;
y0 = _root[clip].y/_root[clip].z +ystart;
_root[clip]._x=x0;
_root[clip]._y=y0;
_root[clip]._xscale = (1 - ( _root[clip].z / maxdist ))*100*scalefac ;
_root[clip]._yscale = (1 - ( _root[clip].z / maxdist ))*100*scalefac ;
//deg = math.asin(math.abs(width/2-_root[clip].x)/math.sqrt(_root[clip].z*_root[clip].z+(_root[clip].x-width/2)*(_root[clip].x-width/2)))
//deg = deg/math.PI * 180;
deg = math.abs(_root[clip]._x-width/2)/(width/2)*60;
if(_root[clip].x-width/2>0) deg = 360-deg;
deg += 10;
rotate(clip, deg);
_root[clip].swapDepths((maxdist-_root[clip].z)*10+1000+math.random()*10);
}
else {_root[clip].delay--;
_root[clip]._visible = false;}
}
initstarfield(); //initstarfield once
}
onClipEvent(enterFrame){
updatestarfield(); //update starfield
}