一个flash鼠标特效

有点像“水果忍者”划过屏幕时的动画,在每次按住鼠标左键不放然后拖动鼠标并释放,会出现动画。动画我参考别人的代码,然后在显示特效的时候使用了不同的颜色。

代码并不复杂,主要用到了GlowFilter,然后让线条的粗细大小从指定的一个值,慢慢减小..

MouseEffectDemo.as

1: package  

       2: {

       3:     import flash.display.Sprite;

       4:     import flash.events.Event;

       5:     import flash.events.MouseEvent;

       6:     import flash.filters.GlowFilter;

       7:     /**

       8:      * ...

       9:      * @author Meteoric_cry

      10:      */

      11:     public class MouseEffectDemo extends Sprite

      12:     {

      13:         private var _scene:Sprite;

      14:         

      15:         private var _startX:int;

      16:         private var _startY:int;

      17:         

      18:         private var _isDown:Boolean;

      19:         

      20:         private static const MIN_POINT_DISTANCE:int = 20;

      21:         

      22:         public function MouseEffectDemo() 

      23:         {

      24:             initView();

      25:         }

      26:         

      27:         private function initView():void

      28:         {

      29:             _scene = new Sprite();

      30:             

      31:             addChild(_scene);

      32:             

      33:             _scene.filters = [getGlowFilter(0x00CCFF)];

      34:             

      35:             stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);

      36:             stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);

      37:         }

      38:         

      39:         private function listenerEnterFrameHandler():void

      40:         {

      41:             unlistenerEnterFrameHandler();

      42:             

      43:             stage.addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

      44:         }

      45:         

      46:         private function unlistenerEnterFrameHandler():void

      47:         {

      48:             stage.removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

      49:         }

      50:         

      51:         private function onEnterFrameHandler(e:Event):void 

      52:         {

      53:             //LineArr的长度可能会动态变化,详情参见它的update方法

      54:             for each (var line:Line in Line.lineArr)

      55:             {

      56:                 line.update();

      57:             }

      58:             

      59:             update();            

      60:         }

      61:         

      62:         private function update():void

      63:         {

      64:             if (_isDown == false)

      65:             {

      66:                 if (Line.lineArr.length === 0)

      67:                 {

      68:                     unlistenerEnterFrameHandler();

      69:                 }

      70:                 

      71:                 return ;

      72:             }

      73:             

      74:             var endX:int = mouseX;

      75:             var endY:int = mouseY;

      76:             

      77:             if (Math.abs(_startX - endX) < MIN_POINT_DISTANCE || Math.abs(_startY - endY) < MIN_POINT_DISTANCE)

      78:             {

      79:                 return ;

      80:             }

      81:             

      82:             var line:Line = new Line(_startX, _startY, endX, endY);

      83:             

      84:             _scene.addChild(line);

      85:             

      86:             _startX = endX;

      87:             _startY = endY;            

      88:         }

      89:         

      90:         private function onMouseUpHandler(e:MouseEvent):void 

      91:         {

      92:             _isDown = false;

      93:         }

      94:         

      95:         private function onMouseDownHandler(e:MouseEvent):void 

      96:         {

      97:             _isDown = true;

      98:             

      99:             changeColorHandler();

     100:             

     101:             _startX = mouseX;

     102:             _startY = mouseY;

     103:             

     104:             listenerEnterFrameHandler();

     105:         }

     106:         

     107:         private function getGlowFilter(color:Number):GlowFilter

     108:         {

     109:             return new GlowFilter(color, 1, 10, 10, 2, 1, false, false)

     110:         }

     111:         

     112:         private function changeColorHandler():void

     113:         {

     114:             var colorArr:Array = [0x00CCFF, 0xF5001D, 0xBC0093, 0x00CE00, 0xFF7400];

     115:             var color:Number = colorArr[Math.floor(Math.random() * colorArr.length)];

     116:             

     117:             _scene.filters = [getGlowFilter(color)];

     118:         }

     119:         

     120:     }

     121:  

     122: }

Line.as

1: package  

       2: {

       3:     import flash.display.Sprite;

       4:     

       5:     /**

       6:      * ...

       7:      * @author Meteoric_cry

       8:      */

       9:     public class Line extends Sprite

      10:     {

      11:         static public var lineArr:Array = [];

      12:         

      13:         public var startX:int;

      14:         public var startY:int;

      15:         public var endX:int;

      16:         public var endY:int;

      17:         public var lineSize:int;

      18:         

      19:         public function Line(x0:int, y0:int, x1:int, y1:int, size:int=8) 

      20:         {

      21:             lineArr.push(this);

      22:             

      23:             startX = x0;

      24:             startY = y0;

      25:             endX = x1;

      26:             endY = y1;

      27:             lineSize = size;

      28:             

      29:             draw();

      30:         }

      31:         

      32:         private function draw():void

      33:         {

      34:             graphics.clear();

      35:             graphics.lineStyle(lineSize, 0xFFFFFF);

      36:             graphics.moveTo(startX, startY);

      37:             graphics.lineTo(endX, endY);

      38:         }

      39:         

      40:         public function update():void

      41:         {

      42:             draw();

      43:             

      44:             lineSize -= 1;

      45:             

      46:             if (lineSize < 1)

      47:             {

      48:                 clear();

      49:             }

      50:         }

      51:         

      52:         private function clear():void

      53:         {

      54:             graphics.clear();

      55:             

      56:             for (var i:int = 0, len:int = lineArr.length; i < len; i++)

      57:             {

      58:                 var line:Line = lineArr[i] as Line;

      59:                 

      60:                 if (line == this)

      61:                 {

      62:                     lineArr.splice(i, 1);

      63:                     parent.removeChild(this);

      64:                     

      65:                     break;

      66:                 }

      67:             }

      68:         }

      69:         

      70:         

      71:     }

      72:  

      73: }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏lonelydawn的前端猿区

前端验证码绘制(canvas)

一切尽在代码中 js文件 /** * canvas绘制动画浮动验证码 * code by lonelydawn 2017-04-10 */ var c...

3427
来自专栏菩提树下的杨过

发布一个轻量级的滑块控件

比系统自带的组件体积要小很多,而且支持进度条显示(在做播放器时,显示缓冲进度很有用哦),另外也支持三角形的音量调整显示 使用示例: package { imp...

3928
来自专栏Golang语言社区

使用WebAssembly和Go编写前端Web框架

JavaScript Frontend frameworks have undoubtedly helped to push the boundaries of...

3483
来自专栏GIS讲堂

openlayers2渐变色渲染

在前文中,讲到了oL2中唯一值渲染的实现方式,在本文讲述ol2中渐变色渲染的实现方式。

2022
来自专栏叁金大数据

EmguCV学习——简单算法 差分与高斯

公司项目需要检测运动物体,我对opencv也没啥研究,google了好久看了好多方法,最简单的就是差分与高斯背景建模了。

1723
来自专栏GIS讲堂

Openlayers中热力图的实现

Heatmap 是用来呈现一定区域内的统计度量,最常见的网站访问热力图就是以特殊高亮的形式显示访客热衷的页面区域和访客所在的地理区域的图示。Heatmap.j...

5423
来自专栏码匠的流水账

使用BufferedImage进行渐变色操作

1501
来自专栏PPV课数据科学社区

【学习】七天搞定SAS(三):基本模块调用

搞定基本的函数之后,开始鼓捣SAS里面的模型。也就是说,要开始写PROC了。说实话,越学SAS,越觉得SAS像Stata...无论是从输出的样式,还是语法。好不...

3385
来自专栏菩提树下的杨过

Flash/Flex学习笔记(40):弹性运动续--弹簧

上一篇里演示的弹性运动加上摩擦力因素后,物体最终基本上都会比较准确的停在目标位置。但是我们回想一下现实世界中的弹簧,如果把弹簧的一头固定起来(即相当于目标点),...

2035
来自专栏菩提树下的杨过

Silverlight Telerik控件学习:TreeView数据绑定并初始化选中状态、PanelBar的Accordion效果、TabPanel、Frame基本使用

实际开发中控件的数据源肯定是动态绑定的,不可能在xaml里写死item项。既然要绑定,就先来几个实体类: ? 上面是类图,各类的代码如下:  Business...

3018

扫码关注云+社区

领取腾讯云代金券