1, brown movement secret http://baike.baidu.com/view/17875.htm
2, in the animation process can be seen everywhere random applications, however, we directly generated by random number of random number is not very uniform, when we want to let the object feel no external force, any sense of uniform distribution (such as: The vast sky, the flying bee flies in the box, too much sunlight dust, etc.), The Brownian movement worked quite well.
3. Basic principle:
3.1 Square distribution (100 pixels each in the center of the stage):
for (Var i:uint=0;i<numcircle;i++) {circle = new ucircle (); circle.x = stage. STAGEWIDTH/2 + math.random () * 100-50; Circle.y = stage. STAGEHEIGHT/2 +math.random () * 100-50; Mycirclepanel.addchild (circle); Circles.push (circle); }
3.2: Circular distribution:
for (Var i:uint=0;i<numcircle;i++) {circle = new ucircle (); var radius:number = Math.sqrt (Math.radom ()) * Maxradius; VA R angle:number = Math.random () * (Math.PI * 2); circle.x = stage. STAGEWIDTH/2 + math.cos (angle) * RADIUS; Circle.y = stage. STAGEHEIGHT/2 + math.sin (angle) * RADIUS; Mycirclepanel.addchild (circle); Circles.push (circle); }
3.3: Biased distribution (tending toward center distribution, more close to center):
for (Var i:uint=0;i<numcircle;i++) {circle = new ucircle (), xpos = 0; ypos = 0; for (var j:uint = 0;j<iterations; j + + )//Six iterations biased evenly distributed {xpos + = Math.random () * stage.stagewidth; Ypos + = Math.random () * stage.stageheight;} circle.x = Xpos/ iterations; Circle.y = ypos/iterations; Mycirclepanel.addchild (circle); Circles.push (circle); }
4. Integrated application (300 flashing little fireflies are drawn in the box):
Package {import flash.display.Sprite; import flash.events.Event, public class Brownclass extends Spri Te {//mymask//mymolecule private var numcircle:uint =; private var iterations:uint = 6; private var Circles:array; p ublic function Brownclass () {init ()} Public Function init (): void {var circle:ucircle; circles = new Array (); var xpos: number; var Ypos:number; for (Var i:uint=0;i<numcircle;i++) {circle = new ucircle (), xpos = 0; ypos = 0; for (var j:uint = 0;j<iterations; j + + )//Six iterations biased evenly distributed {xpos + = Math.random () * stage.stagewidth; Ypos + = Math.random () * stage.stageheight;} circle.x = Xpos/ iterations; Circle.y = ypos/iterations; Mycirclepanel.addchild (circle); Circles.push (circle); } this.addeventlistener (Event.enter_frame,onenterframe); } function Onenterframe (e:event): void {var circle:ucircle; for (var i:uint=0;i<numcircle;i++) {circle = circles[i]; ci Rcle.x + = Math.random () * 0.8-0.4; Circle.y + = Math.random () * 0.8-0.4; Confine the dots to the inside of the box if (circle.x<mymask.x) {circle.x=mymask.x} else if (circle.x> (mymask.x + mymask.width-circle.width)) {circle.x = mymask.x + mymask.width-circle.width; } if (CIRCLE.Y<MYMASK.Y) {circle.y=mymask.y} else if (circle.y> (mymask.y + mymask.height-circle.height)) {Circle . y= mymask.y + mymask.height-circle.height; } } } } }
5. Demo Demo and source code: http://www.flash8.net/fla/10071.shtml http://www.flash8.net/fla/10071.shtml