PDA

View Full Version : Swarming


JerryScript
05-30-2004, 01:28 PM
I've was messing around with eliptical orbits, and came up with a basic swarming prototype. I created it in Ming, so it may need a bit of tweeking for the Flash IDE. Example (http://jerryscript.hostrocket.com/flash/examples/particle/swarm-html.php)

MovieClip.prototype.swarm=function(sides,start,cha nge){
this.c=start;
this.bs=start-180;
this.bsc=change;
this.bsx=1;
this.ly=0;
this.fly=this.flyout=0;
this.sides=sides;
this.frozen=0;
this.onEnterFrame=function(){
if(this.frozen==0){
if(this.flyout<2){this.flyout+=0.01;}else{if(Math.round(Math.rand om()*200)==1){this.flyout=0;}}
this.fly=(this.flyout<1)?2-this.flyout:this.flyout;
this.clear();
if(this.c<=360){
if(this.bs<-180 || this.bs>180){this.bsc*=(-1);}
if(this.bs>180){this.bsx*=(-1);}
this.bs+=this.bsc;
if(this.bsx<0){this.xx=this.bs;this.yy=180;}else{this.xx=180;t his.yy=this.bs;}
this.moveTo(this.x,this.y);
for(a=0;a<=10;a+=2){
this.lineStyle(a,0xaaaaaa,a/10*100/4);
this.x=Math.sin(Math.PI/180*(this.c+a))*this.xx/this.fly;
this.y=Math.cos(Math.PI/180*(this.c+a))*this.yy/this.fly;
this.lineTo(this.x,this.y);
}
this.c+=360/this.sides;
}else{
this.c=0;
}
updateAfterEvent();
if(Math.round(Math.random()*200)==1 && (this.x*this.x+this.y*this.y)<90*90){
this.frozen=Math.round(Math.random()*40);
}
}else{
this.frozen--;
}
}
}

// example usage, create a light sphere to swarm around
createEmptyMovieClip('sphere',1);
with(sphere){
lineStyle(1,0x0000ff,1);
colors =[ 0xffffff,0xffffff,0xffffff ];
alphas =[ 100,20,25 ];
ratios =[ 0,254,255 ];
matrix ={matrixType:'box',x:-78,y:-78,w:155,h:155,r:(45/180)*Math.PI };
beginGradientFill('radial',colors,alphas,ratios,ma trix );
for(c=0;c<360;c+=6){
x=Math.sin(Math.PI/180*c)*85;
y=Math.cos(Math.PI/180*c)*85;
if(c==0){moveTo(x,y);}
lineTo(x,y);
_x=450;
_y=250;
}
endFill();

// create the swarm, here there are 8 "bugs"
for(m=1;m<9;m++){
sphere.createEmptyMovieClip('dot'+m,1+m);
sphere['dot'+m].swarm(60,Math.round(Math.random()*360),m);
}
}

_root.sphere.onPress=function(){
this.startDrag();
}

_root.sphere.onRelease=_root.sphere.onReleaseOutsi de=function(){
stopDrag();
}

// this will make the sphere flicker and enables scaling
_root.sphere.onEnterFrame=function(){
this._alpha=Math.random()*15+85;
this._xscale=this._yscale=_root.slider._y-200;
}

// create a slider for scaling
_root.lineStyle(4,0xffffff);
for(d=1;d<6;d++){
_root.moveTo(95,d*50+150);
_root.lineTo(105,d*50+150);
}
_root.moveTo(100,200);
_root.lineTo(100,400);
createEmptyMovieClip('slider',100);
with(slider){
lineStyle(5,0xffffff);
moveTo(-10,0);
lineTo(10,0);
_x=100;
_y=250;
}

_root.slider.onPress=function(){
this.startDrag(1,100,200,100,400);
}

_root.slider.onRelease=_root.slider.onReleaseOutsi de=function(){
stopDrag();
}


The slider is for scale, and the shpere is draggable.

cobo
06-01-2004, 06:44 AM
transmitting it to the movies section will make it easier to find for others...

anyway, thanx for that.

cobo

JerryScript
06-01-2004, 07:10 PM
Thanks for the tip, I guess I'm confused about the sections, never would of thought of posting it to movies :)