Ï£Íû´ó¼ÒÌá³öÒâ¼û¡£Ð޸ġ£·Ï»°ÉÙ˵£¬ÏÈ¿´Ð§¹û£º
²Ù×÷Àࣺ
package com.mygamemylove{ import flash.display.DisplayObject; import flash.display.Graphics; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; import flash.geom.Rectangle; public class opObject { private var spOp:Sprite; private var currentOp:Sprite; private var arrPoint:Array; // private var isDrogOp:Boolean; private var isDrog:Boolean; private var pointType:uint; private var spPoint:Sprite; // private var sizeType:Number; private var minW:Number; private var minH:Number; private var drawC:uint; private var spParent:Sprite; //Ðýת private var isR:Boolean public var nowRotation:Number; public var pCent:Point; public var spRotationPoint:Sprite; public var spCenterPoint:Sprite; public var numOpPointWH:uint; /** * ²Ù×÷Ò»¸ö¶ÔÏ󣬿ÉÒÔ·Å´óËõС£¬Ðýת¡£ * *Ô´´flash´úÂ룬¾¡ÔÚ×ÔÓé×ÔÀÖ www.MyGameMyLove.com *smallerbird smallerbird@gmail.com 2009-9-12 * * * @spParent:Sprite ²Ù×÷¶ÔÏóµÄ¸¸¶ÔÏó * @currentOp:Sprite ÆäÖвÙ×÷µÄÒ»¸ö¶ÔÏó * @sizeType:Number µ÷Õû³ß´çµÄÏÔʾģʽ¡£ * @minW:Number ËõС×îС³ß´ç¿í * @minH:Number ËõС×îС³ß´ç¸ß * @drawC:Number ²Ù×÷±úµÄÉ« * @numOpPointWH:Number ²Ù×÷±úµÄ¿í¸ß * */ public function opObject(spParent:Sprite,currentOp:Sprite, sizeType:Number=1, minW:Number=10, minH:Number=10, drawC:Number=0xff0000, numOpPointWH:uint=10){ this.numOpPointWH=numOpPointWH; this.currentOp=currentOp this.spParent=spParent; this.spParent.mouseEnabled=false; this.sizeType=sizeType; this.minW=minW; this.minH=minH; isDrogOp=false; isDrog=false; pointType=0; arrPoint=new Array(); nowRotation=0; isR=true } Ô´´úÂëÏÂÔصØÖ·£ºhttp://www.mygamemylove.com/bbs/viewthread.php?tid=48 //¸Ä±ä×¢²áµã public static function RegPoint($obj:Sprite, $point:Point):void { var tmp_point:Point=$obj.parent.globalToLocal($obj.localToGlobal($point)); var len:int=$obj.numChildren; while (len--) { var tmp_obj:DisplayObject=$obj.getChildAt(len); tmp_obj.x-=$point.x; tmp_obj.y-=$point.y; } $obj.x=tmp_point.x; $obj.y=tmp_point.y; } private function drawR(g:Graphics, c:uint, x:Number, y:Number, w:Number, h:Number):void { g.beginFill(c, 0.5); g.drawRoundRect(x, y, w, h, 5); g.endFill(); } private function drawOpPont(sp:Sprite, x:Number, y:Number, c:uint, w:Number, h:Number):void { var spTem:Sprite=new Sprite(); spTem.x=x; spTem.y=y; drawR(spTem.graphics, c, -w / 2, -h / 2, w, h); sp.addChild(spTem); arrPoint.push(spTem); } // private function clrPointSize():void { if (arrPoint.length != 0) { for (var i:uint=0; i < arrPoint.length; i++) { arrPoint[i].removeEventListener(MouseEvent.MOUSE_DOWN, fun_point_down); arrPoint[i].removeEventListener(MouseEvent.MOUSE_UP, fun_point_up); spParent.removeChild(arrPoint[i]); } arrPoint=new Array(); } spParent.graphics.clear(); } // private function clrPoint():void { clrPointSize(); if (spCenterPoint != null) { spParent.removeChild(spCenterPoint); spCenterPoint=null; spParent.removeChild(spRotationPoint); } } // private function showOp4point(sp:Sprite):void { // clrPoint(); var r:Rectangle=sp.getRect(spParent); // var x1:Number=r.x; var y1:Number=r.y; var w1:Number=r.width; var h1:Number=r.height; var w2:Number=w1 / 2; var h2:Number=h1 / 2; // var c:uint=drawC; var p_tem:Sprite=spParent; // pCent=new Point(x1 + w2, y1 + h2); // drawOpPont(p_tem, x1, y1, c, numOpPointWH, numOpPointWH); drawOpPont(p_tem, x1 + w2, y1, c, numOpPointWH, numOpPointWH); drawOpPont(p_tem, x1 + w1, y1, c, numOpPointWH, numOpPointWH); // drawOpPont(p_tem, x1 + w1, y1 + h2, c, numOpPointWH, numOpPointWH); drawOpPont(p_tem, x1 + w1, y1 + h1, c, numOpPointWH, numOpPointWH); // drawOpPont(p_tem, x1 + w2, y1 + h1, c, numOpPointWH, numOpPointWH); drawOpPont(p_tem, x1, y1 + h1, c, numOpPointWH, numOpPointWH); drawOpPont(p_tem, x1, y1 + h2, c, numOpPointWH, numOpPointWH); for (var i:uint=0; i < arrPoint.length; i++) { arrPoint[i].addEventListener(MouseEvent.MOUSE_DOWN, fun_point_down); arrPoint[i].addEventListener(MouseEvent.MOUSE_UP, fun_point_up); } // //»Á¬Ïß var gTem:Graphics=p_tem.graphics; gTem.clear(); gTem.lineStyle(1, c, 0.5); gTem.moveTo(arrPoint[0].x, arrPoint[0].y); for (i=1; i < arrPoint.length; i++) { gTem.lineTo(arrPoint[i].x, arrPoint[i].y); } gTem.lineTo(arrPoint[0].x, arrPoint[0].y); gTem.lineTo(arrPoint[4].x, arrPoint[4].y); gTem.moveTo(arrPoint[6].x, arrPoint[6].y); gTem.lineTo(arrPoint[2].x, arrPoint[2].y); // gTem.moveTo(arrPoint[1].x, arrPoint[1].y); gTem.lineTo(arrPoint[5].x, arrPoint[5].y); // gTem.moveTo(arrPoint[7].x, arrPoint[7].y); gTem.lineTo(arrPoint[3].x, arrPoint[3].y); //»ÐýתµÄµã //ÖÐÐĵã if(isR){ spCenterPoint=new Sprite(); spCenterPoint.mouseEnabled=false; spCenterPoint.graphics.beginFill(0xff0000, 0.5); spCenterPoint.graphics.drawCircle(0, 0, numOpPointWH / 2); spCenterPoint.graphics.endFill(); spCenterPoint.x=pCent.x; spCenterPoint.y=pCent.y; var pTem:Point=currentOp.globalToLocal(pCent); spParent.addChild(spCenterPoint); //Ðýת¿ØÖƵã spRotationPoint=new Sprite(); spRotationPoint.graphics.beginFill(0xff0000, 0.5); spRotationPoint.graphics.drawCircle(0, 0, numOpPointWH / 2); spRotationPoint.graphics.endFill(); spRotationPoint.x=x1 - numOpPointWH; spRotationPoint.y=y1 - numOpPointWH; spParent.addChild(spRotationPoint); spRotationPoint.addEventListener(MouseEvent.MOUSE_DOWN, fun_point_down); spRotationPoint.addEventListener(MouseEvent.MOUSE_UP, fun_point_up); } // } public function fun_opUp(e:MouseEvent):void { isDrogOp=false; currentOp.stopDrag(); showOp4point(currentOp); } //Èç¹ûisR=false ²»¿ÉÒÔ½øÐÐÐýת²Ù×÷ public function fun_opDown(e:MouseEvent,isR:Boolean=true):void { this.isR=isR currentOp=e.target as Sprite; showOp4point(currentOp); // currentOp.startDrag(); isDrogOp=true; } public function fun_over(e:MouseEvent):void { var spTem:Sprite=e.target as Sprite; } // //²»ÄÜÔ½¹ý±ß½ç public function noMoveBorder(rBorder:Rectangle):uint { var r:Rectangle = currentOp.getBounds(spParent) var numOffsetTem:Number=10 if(rBorder.width-numOffsetTem<r.width){ currentOp.width=rBorder.width-numOffsetTem return 0 } if(rBorder.height-numOffsetTem<r.height){ currentOp.height=rBorder.height-numOffsetTem return 0 } //trace(currentOp); if (r.x<rBorder.x) { opObject.RegPoint(currentOp, currentOp.globalToLocal(pCent)); no(); currentOp.x=rBorder.x+r.width/2; } if (r.y<rBorder.y) { opObject.RegPoint(currentOp, currentOp.globalToLocal(pCent)); no(); currentOp.y=rBorder.y+r.height/2; } if (r.x+r.width>rBorder.x+rBorder.width) { opObject.RegPoint(currentOp, currentOp.globalToLocal(pCent)); no(); currentOp.x=rBorder.x+rBorder.width-r.width/2; } if (r.y+r.height>rBorder.y+rBorder.height) { opObject.RegPoint(currentOp, currentOp.globalToLocal(pCent)); no(); currentOp.y=rBorder.y+rBorder.height-r.height/2; } return 0 } //È¡ÏûµÄËùÓж¯×÷ public function no():void { isDrog=false; clrPoint(); if (spPoint) { spPoint.stopDrag(); } currentOp.stopDrag(); } //ÖØÐÂÉèÖÃ×¢²áµã private function seCentXY():void { var pTem:Point; switch (pointType) { case 1 : pTem=currentOp.globalToLocal(new Point(arrPoint[4].x, arrPoint[4].y)); break; case 2 : pTem=currentOp.globalToLocal(new Point(arrPoint[5].x, arrPoint[5].y)); break; case 3 : pTem=currentOp.globalToLocal(new Point(arrPoint[6].x, arrPoint[6].y)); break; case 4 : pTem=currentOp.globalToLocal(new Point(arrPoint[7].x, arrPoint[7].y)); break; case 5 : pTem=currentOp.globalToLocal(new Point(arrPoint[0].x, arrPoint[0].y)); break; case 6 : pTem=currentOp.globalToLocal(new Point(arrPoint[1].x, arrPoint[1].y)); break; case 7 : pTem=currentOp.globalToLocal(new Point(arrPoint[2].x, arrPoint[2].y)); break; case 8 : pTem=currentOp.globalToLocal(new Point(arrPoint[3].x, arrPoint[3].y)); break; } opObject.RegPoint(currentOp, pTem); } private function fun_point_down(e:MouseEvent):void { // var spTem:Sprite=e.target as Sprite; spPoint=spTem; if (spRotationPoint == spPoint) { var dx:Number=currentOp.parent.mouseX - pCent.x; var dy:Number=currentOp.parent.mouseY - pCent.y; nowRotation-=(Math.atan2(dy, dx) * 180 / Math.PI); var pTem:Point=currentOp.globalToLocal(pCent); opObject.RegPoint(currentOp, pTem); spTem.alpha=0; clrPointSize(); } else { pointType=0; for (var i:uint=0; i < arrPoint.length; i++) { if (arrPoint[i] == spTem) { pointType=i + 1; break; } } seCentXY(); } isDrog=true; spTem.startDrag(true); } private function fun_point_up(e:MouseEvent):void { nowRotation=currentOp.rotation; clrPoint(); } private function isUpObj(sp:Sprite):Boolean { var isRe:Boolean=false; if (currentOp == sp) { isRe=true; } else { for (var i:uint=0; i < arrPoint.length; i++) { if (arrPoint[i] == sp) { isRe=true; break; } } // if (spRotationPoint == sp) { isRe=true; } } return isRe; } public function fun_Mouse_up(e:MouseEvent):void { isDrog=false; if (!isUpObj(e.target as Sprite)) { clrPoint(); } else { if (spPoint) { spPoint.stopDrag(); } } } public function fun_onEnterFrame(e:Event):Boolean { if (isDrogOp) { showOp4point(currentOp); } if (!isDrog) { return false; } var spTem:Sprite=spPoint; var dx:Number, dy:Number; //Ðýת if (spRotationPoint == spPoint) { dx=currentOp.parent.mouseX - pCent.x; dy=currentOp.parent.mouseY - pCent.y; currentOp.rotation=(Math.atan2(dy, dx) * 180 / Math.PI) + nowRotation; return true; } //·Å´ó/////////////// switch (pointType) { case 1 : dx=arrPoint[4].x - spTem.x; dy=arrPoint[4].y - spTem.y; break; case 2 : dx=0; dy=arrPoint[5].y - spTem.y; break; case 3 : dx=spTem.x - arrPoint[6].x; dy=arrPoint[6].y - spTem.y; break; case 4 : dx=spTem.x - arrPoint[7].x; dy=0; break; case 5 : dx=spTem.x - arrPoint[0].x; dy=spTem.y - arrPoint[0].y; break; case 6 : dx=0; dy=spTem.y - arrPoint[1].y; break; case 7 : dx=arrPoint[2].x - spTem.x; dy=spTem.y - arrPoint[2].y; break; case 8 : dx=arrPoint[3].x - spTem.x; dy=0; break; } if (dx > minW) { currentOp.width=dx; } if (dy > minH) { currentOp.height=dy; } showOp4point(currentOp); return true; } } } |
ÌÖÂÛ: http://www.missyuan.com/viewthread.php?tid=420172