有點像“水果忍者”劃過屏幕時的動畫,在每次按住鼠標左鍵不放然後拖動鼠標並釋放,會出現動畫。動畫我參考別人的代碼,然後在顯示特效的時候使用了不同的顏色。
代碼並不複雜,主要用到了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: }