2011-07-21 78 views
0

我有一個保存位圖數據的精靈。 我想讓用戶能夠用滑塊調整圖像的大小。 我正在使用下面的代碼,因爲您可以看到問題在於縮放比例是非常重要的,因此圖像完全消失。AS3:使用矩陣滑塊更改縮放精靈?

據我所知,我必須以非疊加的方式對其進行縮放,只是無法弄清楚如何?

我試圖通過:

var m:Matrix = userImageCopy.transform.matrix; 

時userImageCopy保持原始圖像。這有助於縮放,但隨後,每次縮放開始時,userImage跳轉到userImageCopy的位置。

任何幫助?

function onSliderChange(evt:Event):void 
    { 
     trace(evt.target.value); 
     //this will create a point object at the center of the display object 

     var ptRotationPoint:Point = new Point(userImage.x + userImage.width/2,userImage.y + userImage.height/2); 
     //call the function and pass in the object to be rotated, the amount to scale X and Y (sx, sy), and the point object we created 
     scaleFromCenter(userImage, evt.target.value, evt.target.value, ptRotationPoint); 
    } 

    private function scaleFromCenter(ob:*, sx:Number, sy:Number, ptScalePoint:Point) 
    { 
     var m:Matrix = userImage.transform.matrix; 
     m.tx -= ptScalePoint.x; 
     m.ty -= ptScalePoint.y; 
     m.scale(sx, sy); 
     m.tx += ptScalePoint.x; 
     m.ty += ptScalePoint.y; 
     ob.transform.matrix = m; 
    } 

回答

0

而不是你的中心值與當前的tx和ty結合,只是將它們直接與Matrix.translate方法:

private function scaleFromCenter(ob:*, sx:Number, sy:Number, ptScalePoint:Point) 
{ 
    var m:Matrix = new Matrix(); 
    m.translate(-ptScalePoint.x,-ptScalePoint.y); 
    m.scale(sx, sy); 
    m.translate(ptScalePoint.x,ptScalePoint.y); 
    ob.transform.matrix = m; 
}