当前位置: 主页 > 网页制作 > Flash > as制作小球连线代码

as制作小球连线代码

时间:2010-1-12来源:互联网 点击:

很简单的效果,是在受到书中启发做出来的,不过效率不高,回头看看书上是怎么实现的,代码都是自己写的,发现这个效果很简单其实


  小球代码

package
{
    import flash.display.CapsStyle;
    import flash.display.JointStyle;
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.display.Shape;
    public class Ball extends Shape
    {
        public var radius=10;
        public var vx=0;
        public var vy=0;
        public function Ball()
        {
            this.draw();
            this.x=this.radius;
            this.y=this.radius;
        }
        function draw()
        {
            this.graphics.beginFill(0x336FE9,.7)
        
            this.graphics.drawCircle(0,0,this.radius);
            this.graphics.endFill();
            trace("square innit");
        }
        function move()
        {
            
            this.x+=this.vx;
            this.y+=this.vy;
            if(this.x>this.stage.stageWidth||this.x<0)
            this.vx=-this.vx;
            if(this.y>this.stage.stageHeight||this.y<0)
            this.vy=-this.vy;
        }
    }
}

myAS类,我自己写的类

package {
    import flash.display.Sprite;

    public class myAS extends Sprite
    {
        public function myAS()
        {
            for(var i=0;i<10;i++)
            {
                rand(5);
            }
        }
        //返回一个随机值
        public static function rand(x)
        {
            var returnvar=Math.ceil(Math.random()*(x));
            trace(returnvar);
              return returnvar;
        }
        //产生带有负象限的随机值
        public static function randNeg(x){
                if(Math.random()<0.5) 
                    return Math.ceil(Math.random()*x)*(-1);
               else return Math.ceil(Math.random()*x);
           }
           
    }
}

主代码

package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.TimerEvent;
    import flash.utils.Timer;


[SWF(width=640, height=480, backgroundColor=0x111111, frameRate=30)]
    public class ball_demo extends Sprite
    {
        public function ball_demo()
        {
            this.stage.scaleMode = StageScaleMode.NO_SCALE;
            this.stage.align = StageAlign.TOP_LEFT;
            var ballCount=100;
            var ballArray=new Array();
            
            for(var i=0;i<ballCount;i++)
            {
                ballArray[i]=new Ball();
                
                this.addChild(ballArray[i]);
                ballArray[i].x=myAS.rand(600)+20;
                ballArray[i].y=myAS.rand(440)+20;
                var scale=myAS.rand(5)*0.1;
                ballArray[i].scaleX=scale;
                ballArray[i].scaleY=scale;
                ballArray[i].vx=myAS.randNeg(5);
                ballArray[i].vy=myAS.randNeg(5);
            }
            var this1=this;
            function move()
            {
                this1.graphics.clear();
                for(var i=0;i<ballCount;i++)
                {
                    ballArray[i].move();
                    for(var j=0;j<ballCount;j++)
                    {
                        var lengthx=ballArray[i].x-ballArray[j].x;
                        var lengthy=ballArray[i].y-ballArray[j].y;
                        var length=Math.sqrt(lengthx*lengthx+lengthy*lengthy);

//当距离小于60时,画连线
                        if(length<60){
                            this1.graphics.lineStyle(1,0xffffff,1/(length*0.1));
                            this1.graphics.moveTo(ballArray[i].x,ballArray[i].y);
                            this1.graphics.lineTo(ballArray[j].x,ballArray[j].y);
                        }
                    }
                }
            }
            var time1:Timer=new Timer(20)
            time1.addEventListener(TimerEvent.TIMER,move);
            time1.start();
        }
    }
}

站长资讯网
. TAG: as 小球连线
推荐内容最近更新人气排行
关于我们 | 友情链接 | 网址推荐 | 常用资讯 | 网站地图 | RSS | 留言