zoukankan      html  css  js  c++  java
  • 布朗(随机)运动

    基础:Math.random() * 200 - 100;

    代码
    package
    {
        
    import flash.display.Sprite;
        
    import flash.events.Event;

        
    public class Main extends Sprite
        {
            
    private var numDots:uint = 20;
            
    private var friction:Number = 0.95;
            
    private var dots:Array;

            
    public function Main()
            {
                init();
            }

            
    private function init():void
            {
                graphics.lineStyle(
    00, .5);
                dots 
    = new Array();
                
    for (var i:uint = 0; i < numDots; i++)
                {
                    var dot:Ball 
    = new Ball();
                    dot.x 
    = Math.random() * stage.stageWidth;
                    dot.y 
    = Math.random() * stage.stageHeight;
                    dot.vx 
    = 0;
                    dot.vy 
    = 0;
                    addChild(dot);
                    dots.push(dot);
                }
                addEventListener(Event.ENTER_FRAME, onEnterFrame);
            }

            
    private function onEnterFrame(event:Event):void
            {
                
    for (var i:uint = 0; i < numDots; i++)
                {
                    var dot:Ball 
    = dots[i];
                    graphics.moveTo(dot.x, dot.y);
                    dot.vx 
    += Math.random() * 0.2 - 0.1;
                    dot.vy 
    += Math.random() * 0.2 - 0.1;
                    dot.x 
    += dot.vx;
                    dot.y 
    += dot.vy;
                    dot.vx 
    *= friction;
                    dot.vy 
    *= friction;
                    graphics.lineTo(dot.x, dot.y);
                    
    if (dot.x > stage.stageWidth)
                    {
                        dot.x 
    = 0;
                    }
                    
    else if (dot.x < 0)
                    {
                        dot.x 
    = stage.stageWidth;
                    }
                    
    if (dot.y > stage.stageHeight)
                    {
                        dot.y 
    = 0;
                    }
                    
    else if (dot.y < 0)
                    {
                        dot.y 
    = stage.stageHeight;
                    }
                }
            }
        }
    }
    import flash.display.Sprite;
    class Ball extends Sprite
    {
        
    public var vx:Number;
        
    public var vy:Number;
        
    public function Ball():void
        {
            
    this.graphics.beginFill(0);
            
    this.graphics.drawCircle(002);
            
    this.graphics.endFill();
        }
    }
  • 相关阅读:
    Implementation of Message Receiver
    Data Model for Message Receiver
    SQL SERVER 批量生成编号
    修改hosts文件
    PHP-问题处理验证码无法显示出来
    PHP-问题处理Fatal error: Uncaught Error: Call to undefined function mb_strlen()
    PHP-问题处理Fatal error: Uncaught Error: Call to undefined function simplexml_load_file()
    10进制转33进制
    PHP-生成缩略图和添加水印图-学习笔记
    SQLServer地址搜索性能优化例子
  • 原文地址:https://www.cnblogs.com/sevenyuan/p/1678694.html
Copyright © 2011-2022 走看看