as3实现点击舞台,将舞台切割成一个个的小矩形,按下shift键可以改变切割的方向。
demo:
实现代码:
package { import flash.display.Graphics; import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Rectangle; import flash.ui.Mouse; public class Main extends MovieClip { public var cursor:MovieClip; private var rects:Array; private var bg:Sprite; private var rect:Rectangle = new Rectangle(0, 0, 500, 400); public function Main() { // constructor code cursor.mouseEnabled = cursor.mouseChildren = false; bg = new Sprite(); addChild(bg); rects = [rect]; updateBg(); stage.addEventListener(MouseEvent.CLICK, mouseClickHandler); Mouse.hide(); stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } private function mouseMoveHandler(e:MouseEvent):void { cursor.x = mouseX; cursor.y = mouseY; cursor.rotation = e.shiftKey?0:90; e.updateAfterEvent(); } private function mouseClickHandler(e:MouseEvent):void { if (!rect.contains(mouseX,mouseY)) { return; } for (var i:int = 0; i < rects.length;i++ ) { var rect:Rectangle = rects[i]; if (rect.contains(mouseX, mouseY)) { var rect1:Rectangle; var rect2:Rectangle; if (e.shiftKey) { rect1 = new Rectangle(rect.x, rect.y, rect.width, mouseY - rect.y); rect2 = new Rectangle(rect.x,mouseY,rect.width,rect.bottom-mouseY); } else { rect1 = new Rectangle(rect.x, rect.y, mouseX-rect.x, rect.height); rect2 = new Rectangle(mouseX, rect.y, rect.right-mouseX, rect.height); } rects.splice(i, 1, rect1, rect2); break; } } updateBg(); } private function updateBg():void { var g:Graphics =bg.graphics; g.clear(); for (var i:int = 0; i < rects.length;i++ ) { var rect:Rectangle = rects[i]; g.lineStyle(1, 0x000000); g.drawRect(rect.x, rect.y, rect.width, rect.height); } } } }
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。