[英]AS3 image rotate within / scale to fit sprite
我們正在從精靈中獲取位圖數據,我們要在其中拍攝圖像並在/縮放范圍內旋轉以適合。 這是我們的代碼,其中包括旋轉。
_rotation
定義用戶輸入了多少。
問題是,我們得到的輸出文件是100%白色的。 我們認為圖像旋轉約0x0y,因此將圖像旋轉到精靈的邊界之外。
此外,圖像沒有按比例縮放到子級,而是在繼承時進行了“裁剪”。
最好的方法是什么? 基本上,我們要拍攝圖像並在/縮放范圍內旋轉以適合
var sprite1:Sprite = new Sprite();
addChild(sprite1);
var photoBitmap:Bitmap = new Bitmap(_bitmapData);
sprite1.addChild(photoBitmap);
sprite1.rotation = _rotation;
var sprite2:Sprite = new Sprite();
addChild(sprite2);
sprite2.addChild(sprite1);
var bitmapData:BitmapData = new BitmapData(sprite2.width,sprite2.height,false,0xFFFFFF);
bitmapData.draw(sprite2);
通過縮放/旋轉繪制精靈的簡單方法是在bitmapData.draw()方法中使用Matrix 。
例:
var sourceImage:SomeSprite = new SomeSprite();
var matrix:Matrix = new Matrix();
matrix.scale(0.5, 0.5);
matrix.rotate(0.5 * Math.PI);
var newImage:BitmapData = new BitmapData(sourceImage.width/2, sourceImage.height/2);
newImage.draw(sourceImage, matrix);
您的問題可能是圍繞左上角旋轉的原因(這可能使整個對象離開配准點(0 x和0 y)或在其上方,並且無法繪制。
一種簡單的解決方法是旋轉后移動sprite1
,以解決旋轉引起的新大小和位置:
...
sprite2.addChild(sprite1);
var actualPosition:Rectangle = sprite2.getBounds(sprite2); //this gets the new position/dimensions of the object
sprite1.x = -actualPosition.x;
sprite1.y = -actualPosition.y;
var bitmapData:BitmapData = new BitmapData(sprite2.width,sprite2.height,false,0xFFFFFF);
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.