图片没旋转之前拖动很正常。左旋转之后拖动会不正常。那是因为旋转时设置了transform.matrix的原因,那样拖动后摆放时会不正常。
将transform.matrix设置为全局变量,在拖动前将transform.matrix设置为null ,拖动之后再重新获取transform.matrix则可。记得是在设置XY坐标前设置transform.matrix。
主要代码如下:
private var _xOff:Number;
private var _yOff:Number;
public function phandleDrop(event:DragEvent):void
{
var dragaImage:Image = new Image();
dragaImage.source = event.dragSource.dataForFormat( "source" ) as String;
dragaImage.width = event.dragSource.dataForFormat( "width" ) as int;
dragaImage.height = event.dragSource.dataForFormat( "height" ) as int;
var tempX:int = event.currentTarget.mouseX - _xOff ;
var tempY:int = event.currentTarget.mouseY - _yOff ;
dragaImage.transform.matrix = tmpMatrix ;
dragaImage.x = tempX ;
dragaImage.y = tempY ;
myGroup.removeAllElements();
myGroup.addElement( bgRect );
myGroup.addElement( dragaImage );
myGroup.addElement( _mask );
}
private function initJpgDragInCanvas(event:MouseEvent):void
{
var dragInitiator:Image = myGroup.getChildAt( 1 ) as Image;
if ( dragInitiator == null )
{
return ;
}
var ds:DragSource = new DragSource();
ds.addData( dragInitiator, "img" );
ds.addData( dragInitiator.source, "source" );
ds.addData( dragInitiator.width, "width" );
ds.addData( dragInitiator.height, "height" );
var imageProxy:Image = new Image();
imageProxy.source = dragInitiator;
imageProxy.height = 100;
imageProxy.width = 100;
imageProxy.transform.matrix = null;
_xOff = event.currentTarget.mouseX - dragInitiator.x ;
_yOff = event.currentTarget.mouseY - dragInitiator.y ;
DragManager.doDrag( dragInitiator, ds, event, imageProxy, 0, 0 );
}
private var tmpMatrix:Matrix;
private function rotateImage():void{
var img:Image = myGroup.getChildAt( 1 ) as Image;
var matrix:Matrix = new Matrix() ;
tmpMatrix = img.transform.matrix;
var oHeight:Number = img.height / 2;
var oWidth:Number = img.width / 2;
tmpMatrix.translate(-oWidth, -oHeight);
var rotateNum:Number = parseInt( rotateNum.text );
var rot:int = Math.PI / 180 * rotateNum ;
tmpMatrix.rotate( Math.PI / 180 * rotateNum );
tmpMatrix.translate(oWidth, oHeight);
img.transform.matrix = tmpMatrix;
}