zoukankan      html  css  js  c++  java
  • time


    在线演示

    显示代码
     1 package {
     2     import flash.display.Sprite;
     3     import flash.events.Event;
     4     import flash.geom.Point;
     5 
     6     public class MouseAvoider extends Sprite {
     7 
     8         private const SPRING:Number=0.1;//弹性系数
     9         private const FRICTION:Number=0.9;//摩擦系数
    10         private const FEAR_DISTANCE:Number=150;//安全距离(小于该距离则发生躲避行为)
    11         private const MAX_AVOID_FORCE:uint=10;//最大躲避速度
    12 
    13         private var _destinationPoint:Point;//目标静止点(鼠标远离该物体时,物体最终会静止的坐标点)
    14         private var _speed:Point=new Point(0,0);//速度矢量(_speed.x即相当于vx,_speed.y即相当于vy)
    15 
    16         public function MouseAvoider():void {
    17             drawStuff();
    18         }
    19 
    20         private function drawStuff():void {
    21             //默认先画一个半径为10的圆
    22             graphics.beginFill(Math.random() * 0xFFFFFF);
    23             //graphics.beginFill(0xFFFFFF);
    24             graphics.drawCircle(005);
    25             graphics.endFill();
    26         }
    27 
    28         //只写属性(设置目标点)
    29         public function set destinationPoint(value:Point):void {
    30             _destinationPoint=value;
    31             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
    32         }
    33         
    34         
    35         protected function enterFrameHandler(e:Event):void {
    36             moveToDestination();//先移动到目标点
    37             avoidMouse();//躲避鼠标
    38             applyFriction();//应用摩擦力
    39             updatePosition();//更新位置
    40         }
    41 
    42         //以弹性运动方式移动到目标点
    43         private function moveToDestination():void {
    44             _speed.x += (_destinationPoint.x - x) * SPRING;
    45             _speed.y += (_destinationPoint.y - y) * SPRING;
    46         }
    47 
    48         //躲避鼠标
    49         private function avoidMouse():void {
    50             var currentPosition:Point=new Point(x,y);//确定当前位置
    51             var mousePosition:Point=new Point(stage.mouseX,stage.mouseY);//确实鼠标位置
    52             var distance:uint=Point.distance(currentPosition,mousePosition);//计算鼠标与当前位置的距离
    53             
    54             //如果低于安全距离
    55             if (distance<FEAR_DISTANCE) {
    56                 var force:Number = (1 - distance / FEAR_DISTANCE) * MAX_AVOID_FORCE;//计算(每单位时间的)躲避距离--即躲避速率
    57                 var gamma:Number=Math.atan2(currentPosition.y- mousePosition.y,currentPosition.x- mousePosition.x);//计算鼠标所在位置与当前位置所成的夹角
    58                 
    59                 var avoidVector:Point=Point.polar(force,gamma);//将极坐标转换为普通(笛卡尔)坐标--其实相当于vx = force*Math.cos(gamma),vy = force*Math.sin(gamma)
    60                 
    61                 //加速 躲避逃开
    62                 _speed.x+=avoidVector.x;
    63                 _speed.y+=avoidVector.y;
    64             }
    65         }
    66         
    67         //应用摩擦力
    68         private function applyFriction():void {
    69 
    70             _speed.x*=FRICTION;
    71             _speed.y*=FRICTION;
    72         }
    73         
    74         //最终更新自身的位置
    75         private function updatePosition():void {
    76 
    77             x+=_speed.x;
    78             y+=_speed.y;
    79         }
    80 
    81     }
    82 }


    点击看大图

  • 相关阅读:
    测试工具PerfDog的使用
    1.人工智能解读与Python简介
    如何提高百度网盘下载速度小技巧(亲测有效!)
    学习方法
    字符串换行工具类/每隔几位插入指定字符串
    java对pdf文件加文字水印 itextpdf
    centos 7.6 安装jdk8
    1 elk软件的安装
    Springboot 2.2.1 与activeMq 集成2 topic 发布者,订阅者
    Springboot 2.2.1 与activeMq 集成2 queue 消息
  • 原文地址:https://www.cnblogs.com/ddw1997/p/1789914.html
Copyright © 2011-2022 走看看