平时我们选中一个文件或图表时,通常是使用左键单击,鼠标按下拖动会出现一个矩形框,矩形框内的元素都会被选中。下面我们就来模仿一下使用矩形框来选中多个。
先看效果:
代码如下:
package { import flash.display.CapsStyle; import flash.display.GradientType; import flash.display.MovieClip; import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; import flash.ui.Keyboard; public class Main extends MovieClip { private var arr:Array = []; private var choosedArr:Array = []; private var kuang:Sprite = new Sprite(); public function Main() { // constructor code stage.addEventListener(MouseEvent.CLICK, tianjia); stage.addEventListener(MouseEvent.MOUSE_DOWN, kuangxuan); kuang.buttonMode = true; kuang.addEventListener(MouseEvent.MOUSE_DOWN, drag); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyhandler); addEventListener(Event.ENTER_FRAME, enterFramehandler); } private function enterFramehandler(e:Event):void { if (stage.focus==null) { stage.focus = stage; } tf.text = "focus:" + stage.focus; } /** * 按delete键删除 * @param e */ private function keyhandler(e:KeyboardEvent):void { switch(e.keyCode) { case Keyboard.DELETE: for (var i:int = 0; i < choosedArr.length;i++ ) { var sp:Shape = choosedArr[i]; sp.parent.removeChild(sp); } for (i = 0; i < arr.length;i++ ) { if (!arr[i].parent) { arr.splice(i, 1); i--; } } choosedArr = []; kuang.graphics.clear(); break; } } private var kop:Point = new Point(); /** * 选中之后拖动 * @param e */ private function drag(e:MouseEvent):void { switch(e.type) { case MouseEvent.MOUSE_DOWN: e.stopPropagation(); stage.addEventListener(MouseEvent.MOUSE_MOVE, drag); stage.addEventListener(MouseEvent.MOUSE_UP, drag); kop.x = mouseX; kop.y = mouseY; break; case MouseEvent.MOUSE_MOVE: var offsetX:Number = mouseX - kop.x; var offsetY:Number = mouseY - kop.y; kop.x = mouseX; kop.y = mouseY; kuang.x += offsetX; kuang.y += offsetY; for (var i:int = 0; i < choosedArr.length;i++ ) { choosedArr[i].x += offsetX; choosedArr[i].y += offsetY; } break; default: stage.removeEventListener(MouseEvent.MOUSE_UP, drag); stage.removeEventListener(MouseEvent.MOUSE_MOVE, drag); break; } } private var op:Point = new Point(); private var moveFlag:Boolean = false; /** * 框选 * @param e */ private function kuangxuan(e:MouseEvent):void { switch(e.type) { case MouseEvent.MOUSE_DOWN: stage.addEventListener(MouseEvent.MOUSE_UP, kuangxuan); stage.addEventListener(MouseEvent.MOUSE_MOVE, kuangxuan); op.x = mouseX; op.y = mouseY; xuanzhong(false); moveFlag = false; break; case MouseEvent.MOUSE_MOVE: xuanzhong(true); moveFlag = true; break; default: stage.removeEventListener(MouseEvent.MOUSE_UP, kuangxuan); stage.removeEventListener(MouseEvent.MOUSE_MOVE, kuangxuan); break; } } /** * 选中或取消选中 * @param f */ private function xuanzhong(f:Boolean):void { var i:int = 0; var len:int = arr.length; var sp:Shape; if (f) { choosedArr = []; drawRect(); var rect:Rectangle = new Rectangle(); rect.x = Math.min(op.x, mouseX); rect.y = Math.min(op.y, mouseY); rect.width = Math.abs(mouseX - op.x); rect.height = Math.abs(mouseY - op.y); for (i = 0; i < len; i++ ) { sp = arr[i]; if (rect.contains(sp.x,sp.y)) { sp.scaleX = sp.scaleY = 1.2; choosedArr.push(sp); } else { sp.scaleX = sp.scaleY = 1.0; } } } else { kuang.graphics.clear(); for (i = 0; i < len; i++ ) { sp = arr[i]; sp.scaleX = sp.scaleY = 1.0; } } } private function drawRect():void { addChild(kuang); kuang.x = 0; kuang.y = 0; kuang.graphics.clear(); kuang.graphics.lineStyle(1, 0x000000); kuang.graphics.beginFill(0x000000, 0); kuang.graphics.drawRect(op.x, op.y, mouseX - op.x, mouseY - op.y); kuang.graphics.endFill(); } /** * 添加元件 * @param e */ private function tianjia(e:MouseEvent):void { if (moveFlag) return; var sp:Shape = new Shape(); var co:uint = ((Math.random() * 255) << 16) + ((Math.random() * 255) << 8) + (Math.random() * 255); sp.graphics.beginFill(co, Math.random() * 0.5 + 0.5); sp.graphics.drawCircle(0, 0, Math.random() * 5 + 10); sp.graphics.endFill(); addChild(sp); sp.x = mouseX; sp.y = mouseY; arr.push(sp); } } }
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。