2008-05-20

建立 DisplayObject 物件 的 Bitmap 替身

包含動態文字的視覺物件常會因為旋轉而消失
在此弄個類別用以建立替身, 由於是點陣資料, 故縮放的時候會比較醜
測試的 Frame Actions :
import lin.shinder.display.Substitute;

var _txt:TextField = new TextField;
_txt.text = "ABCDEF";
this.addChild(_txt);

var s:Substitute = new Substitute(_txt).displace();
s.rotation = 30;

var s_mc:Substitute = new Substitute(t_mc);
s_mc.x = 200;
s_mc.y = 200;
this.addChild(s_mc);

類別檔 Substitute.as :
package lin.shinder.display
{
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.PixelSnapping;
// 替身
dynamic public class Substitute extends Sprite
{
private var bitmap:Bitmap;
public var bitmapData:BitmapData;
public var original:DisplayObject;

public function Substitute(disp:DisplayObject)
{
original = disp;
bitmapData = new BitmapData(disp.width, disp.height, true, 0);
var rect:Rectangle = disp.getBounds(disp);
var matrix:Matrix = new Matrix;
matrix.translate(-rect.x, -rect.y);
bitmapData.draw(disp, matrix);
bitmap = new Bitmap(bitmapData, PixelSnapping.AUTO, true);
bitmap.x = rect.x;
bitmap.y = rect.y;
this.addChild(bitmap);
this.visible = original.visible;
this.x = original.x;
this.y = original.y;
this.scaleX = original.scaleX;
this.scaleY = original.scaleY;
this.rotation = original.rotation;
}

public function displace():Substitute
{
if(! original.parent) {
trace('The original DisplayObject is not in display chains !');
return null;
}
var _parent:DisplayObjectContainer = original.parent as DisplayObjectContainer;
var depth:int = _parent.getChildIndex(original);
_parent.addChildAt(this, depth);

_parent.removeChild(original);
return this;
}
}
}

測試範例原始檔

2008-05-16

Flash Player 10 beta, code-named "Astro"

新的 Flash Player 10 進入 beta 階段
Developer 另一階段的苦難又要開始了 :P

FB 留言