本教程主要用Flash类来制作一个特效,要用到鼠标操作元件,放大、缩小、旋转、移动。花了一些时间,终于实现了,代码整理了一下,和大家分享一下.
希望大家提出意见。修改。废话少说,先看效果:
操作类:
packagecom.mygamemylove{
importflash.display.DisplayObject; importflash.display.Graphics; importflash.display.Sprite; importflash.events.Event; importflash.events.MouseEvent; importflash.geom.Point; importflash.geom.Rectangle; publicclassopObject{ privatevarspOp:Sprite; privatevarcurrentOp:Sprite; privatevararrPoint:Array; // privatevarisDrogOp:Boolean; privatevarisDrog:Boolean; privatevarpointType:uint; privatevarspPoint:Sprite; // privatevarsizeType:Number; privatevarminW:Number; privatevarminH:Number; privatevardrawC:uint; privatevarspParent:Sprite; //旋转 privatevarisR:Boolean publicvarnowRotation:Number; publicvarpCent:Point; publicvarspRotationPoint:Sprite; publicvarspCenterPoint:Sprite; publicvarnumOpPointWH:uint; /** *操作一个对象,可以放大缩小,旋转。 * *原创flash代码,尽在自娱自乐www.MyGameMyLove.com *smallerbirdsmallerbird@gmail.com2009-9-12 * * *@spParent:Sprite操作对象的父对象 *@currentOp:Sprite其中操作的一个对象 *@sizeType:Number调整尺寸的显示模式。 *@minW:Number缩小最小尺寸宽 *@minH:Number缩小最小尺寸高 *@drawC:Number操作柄的色 *@numOpPointWH:Number操作柄的宽高 **/ publicfunctionopObject(spParent:Sprite,currentOp:Sprite,sizeType:Number=1, 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=newArray(); nowRotation=0; isR=true } 源代码下载地址:http://www.mygamemylove.com/bbs/viewthread.php?tid=48 //改变注册点 publicstaticfunctionRegPoint($obj:Sprite,$point:Point):void{ vartmp_point:Point=$obj.parent.globalToLocal($obj.localToGlobal($point)); varlen:int=$obj.numChildren; while(len–){ vartmp_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; } privatefunctiondrawR(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(); } privatefunctiondrawOpPont(sp:Sprite,x:Number,y:Number,c:uint,w:Number,h:Number):void{ varspTem:Sprite=newSprite(); spTem.x=x; spTem.y=y; drawR(spTem.graphics,c,-w/2,-h/2,w,h); sp.addChild(spTem); arrPoint.push(spTem); } // privatefunctionclrPointSize():void{ if(arrPoint.length!=0){ for(vari: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=newArray(); } spParent.graphics.clear(); } // privatefunctionclrPoint():void{ clrPointSize(); if(spCenterPoint!=null){ spParent.removeChild(spCenterPoint); spCenterPoint=null; spParent.removeChild(spRotationPoint); } } // privatefunctionshowOp4point(sp:Sprite):void{ // clrPoint(); varr:Rectangle=sp.getRect(spParent); // varx1:Number=r.x; vary1:Number=r.y; varw1:Number=r.width; varh1:Number=r.height; varw2:Number=w1/2; varh2:Number=h1/2; // varc:uint=drawC; varp_tem:Sprite=spParent; // pCent=newPoint(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(vari:uint=0;i<arrPoint.length;i++){ arrPoint[i].addEventListener(MouseEvent.MOUSE_DOWN,fun_point_down); arrPoint[i].addEventListener(MouseEvent.MOUSE_UP,fun_point_up); } // //画连线 vargTem: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=newSprite(); 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; varpTem:Point=currentOp.globalToLocal(pCent); spParent.addChild(spCenterPoint); //旋转控制点 spRotationPoint=newSprite(); 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); } // } publicfunctionfun_opUp(e:MouseEvent):void{ isDrogOp=false; currentOp.stopDrag(); showOp4point(currentOp); } //如果isR=false不可以进行旋转操作 publicfunctionfun_opDown(e:MouseEvent,isR:Boolean=true):void{ this.isR=isR currentOp=e.targetasSprite; showOp4point(currentOp); // currentOp.startDrag(); isDrogOp=true; } publicfunctionfun_over(e:MouseEvent):void{ varspTem:Sprite=e.targetasSprite; } // //不能越过边界 publicfunctionnoMoveBorder(rBorder:Rectangle):uint{ varr:Rectangle=currentOp.getBounds(spParent) varnumOffsetTem:Number=10 if(rBorder.width-numOffsetTem<r.width){ currentOp.width=rBorder.width-numOffsetTem return0 } if(rBorder.height-numOffsetTem<r.height){ currentOp.height=rBorder.height-numOffsetTem return0 } //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; } return0 } //取消的所有动作 publicfunctionno():void{ isDrog=false; clrPoint(); if(spPoint){ spPoint.stopDrag(); } currentOp.stopDrag(); } //重新设置注册点 privatefunctionseCentXY():void{ varpTem:Point; switch(pointType){ case1: pTem=currentOp.globalToLocal(newPoint(arrPoint[4].x,arrPoint[4].y)); break; case2: pTem=currentOp.globalToLocal(newPoint(arrPoint[5].x,arrPoint[5].y)); break; case3: pTem=currentOp.globalToLocal(newPoint(arrPoint[6].x,arrPoint[6].y)); break; case4: pTem=currentOp.globalToLocal(newPoint(arrPoint[7].x,arrPoint[7].y)); break; case5: pTem=currentOp.globalToLocal(newPoint(arrPoint[0].x,arrPoint[0].y)); break; case6: pTem=currentOp.globalToLocal(newPoint(arrPoint[1].x,arrPoint[1].y)); break; case7: pTem=currentOp.globalToLocal(newPoint(arrPoint[2].x,arrPoint[2].y)); break; case8: pTem=currentOp.globalToLocal(newPoint(arrPoint[3].x,arrPoint[3].y)); break; } opObject.RegPoint(currentOp,pTem); } privatefunctionfun_point_down(e:MouseEvent):void{ // varspTem:Sprite=e.targetasSprite; spPoint=spTem; if(spRotationPoint==spPoint){ vardx:Number=currentOp.parent.mouseX-pCent.x; vardy:Number=currentOp.parent.mouseY-pCent.y; nowRotation-=(Math.atan2(dy,dx)*180/Math.PI); varpTem:Point=currentOp.globalToLocal(pCent); opObject.RegPoint(currentOp,pTem); spTem.alpha=0; clrPointSize(); }else{ pointType=0; for(vari:uint=0;i<arrPoint.length;i++){ if(arrPoint[i]==spTem){ pointType=i+1; break; } } seCentXY(); } isDrog=true; spTem.startDrag(true); } privatefunctionfun_point_up(e:MouseEvent):void{ nowRotation=currentOp.rotation; clrPoint(); } privatefunctionisUpObj(sp:Sprite):Boolean{ varisRe:Boolean=false; if(currentOp==sp){ isRe=true; }else{ for(vari:uint=0;i<arrPoint.length;i++){ if(arrPoint[i]==sp){ isRe=true; break; } } // if(spRotationPoint==sp){ isRe=true; } } returnisRe; } publicfunctionfun_Mouse_up(e:MouseEvent):void{ isDrog=false; if(!isUpObj(e.targetasSprite)){ clrPoint(); }else{ if(spPoint){ spPoint.stopDrag(); } } } publicfunctionfun_onEnterFrame(e:Event):Boolean{ if(isDrogOp){ showOp4point(currentOp); } if(!isDrog){ returnfalse; } varspTem:Sprite=spPoint; vardx: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; returntrue; } //放大/////////////// switch(pointType){ case1: dx=arrPoint[4].x-spTem.x; dy=arrPoint[4].y-spTem.y; break; case2: dx=0; dy=arrPoint[5].y-spTem.y; break; case3: dx=spTem.x-arrPoint[6].x; dy=arrPoint[6].y-spTem.y; break; case4: dx=spTem.x-arrPoint[7].x; dy=0; break; case5: dx=spTem.x-arrPoint[0].x; dy=spTem.y-arrPoint[0].y; break; case6: dx=0; dy=spTem.y-arrPoint[1].y; break; case7: dx=arrPoint[2].x-spTem.x; dy=spTem.y-arrPoint[2].y; break; case8: dx=arrPoint[3].x-spTem.x; dy=0; break; } if(dx>minW){ currentOp.width=dx; } if(dy>minH){ currentOp.height=dy; } showOp4point(currentOp); returntrue; } } } |
讨论: http://www.missyuan.com/viewthread.php?tid=420172