package { import flash.display.MovieClip; import flash.utils.Timer; import flash.events.TimerEvent; public class RandomAirPort extends MovieClip { private const changeDirectSpeed:Number = 2000;//瓢虫随机转换方向的频率,单位:毫秒 private const updateSpeed:Number = 30;//刷新瓢虫位置的频率,单位:毫秒 private var airWalkSpeed:Number = 1;//瓢虫移动速度,单位:像素 private const PI:Number = Math.PI; private const updateRefreshAngleSpeed:Number = 20;//刷新速度,单位:毫秒 private const refreshAngleSpeed:Number = 0.01;//转换角度的速度 private const zero:Number = 0.001;//自定义接近零的数据 private const stageWidth:Number = 550; private const stageHeight:Number = 400; private var updateTimer:Timer = new Timer(updateSpeed); private var changeDirectTimer:Timer = new Timer(changeDirectSpeed); private var refreshAngleTimer:Timer = new Timer(updateRefreshAngleSpeed);//实现瓢虫转向缓动公式 private var directAngle:Number;//瓢虫移动角度,单位:角度 private var directRadin:Number;//瓢虫移动角度,单位:弧度 private var myWidth:Number = 28; public function RandomAirPort():void { init(); } private function init():void { changeDirectTimer.addEventListener(TimerEvent.TIMER, changeDirect); changeDirectTimer.start(); updateTimer.addEventListener(TimerEvent.TIMER, airPortMove); updateTimer.start(); refreshAngleTimer.addEventListener(TimerEvent.TIMER, refreshAngle); airWalkSpeed = getRandom(0.5,2); } private function changeDirect(evt:TimerEvent):void { getAngle(); refreshAngleTimer.start();//开始转换角度,缓动效果 } private function airPortMove(evt:TimerEvent):void {//瓢虫匀速运动 this.x += airWalkSpeed*Math.cos(this.rotation*PI/180); this.y += airWalkSpeed*Math.sin(this.rotation*PI/180); if (this.x > (stageWidth+myWidth)) { this.x = 0; } else if (this.x < -myWidth) { this.x = stageWidth+myWidth; } if (this.y > (stageHeight+myWidth)) { this.y = 0; } else if (this.y < -myWidth) { this.y = stageHeight+myWidth; } } private function refreshAngle(evt:TimerEvent):void { var distOfAngle = Math.abs(directAngle - this.rotation); if (distOfAngle <= zero) { refreshAngleTimer.stop(); this.rotation = directAngle; } else { var angleDist = refreshAngleSpeed*(directAngle - this.rotation); this.rotation += angleDist; } } private function getAngle() { directAngle = getRandom(-180,180); directRadin = directAngle*PI/180; } private function getRandom(startPara:Number,endPara:Number):Number { var R:Number = Math.random(); var result:Number = startPara + R*(endPara - startPara); return result; } } } |
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |