Make your tiny ions more evenly distributed-Brownian motion

Source: Internet
Author: User
Tags addchild

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&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.