似乎您使用了覆盖图像的 Graphics 对象。为了擦除某些东西,你应该给你的精灵一个最上面的形状,它将 BlendMode.ERASE 作为混合模式,并在其上绘制颜色的 alpha 分量为 0xFF。精灵的 blendMode 应该是 BlendMode.LAYER。这是一个可以玩的测试:
public class TestErase extends Sprite
{
public function TestErase() {
var b:Sprite = new Sprite();
var c:Shape = new Shape();
b.blendMode = BlendMode.LAYER;
c.blendMode = flash.display.BlendMode.ERASE;
c.graphics.beginFill(0, 1);
c.graphics.drawRect(50, 50, 50, 50);
c.graphics.endFill();
b.graphics.beginFill(0xff0000, 1);
b.graphics.drawRect(0, 0, 150, 150);
b.graphics.endFill();
b.addChild(c);
addChild(b);
this.graphics.beginFill(0x00c000, 1);
graphics.drawRect(75, 75, 100, 100);
graphics.endFill();
}
}
这会产生一个 50x50 的孔,由“b”精灵中的“c”精灵定义,它不会影响“this”精灵。
好吧,你问了一个例子。这里。
public class Main extends Sprite
{
public var sh:Shape;
public var drawing:Boolean;
public var sp:Sprite;
public var bd:BitmapData;
public var erasing:Boolean;
[Embed(source = '../lib/093.jpg')]
public static const Background:Class;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
var ba:Bitmap = new Background();
ba.scaleX = 0.5;
ba.scaleY = 0.5;
addChild(ba);
bd = new BitmapData(640, 400, true, 0);
var bm:Bitmap = new Bitmap(bd);
sp = new Sprite();
sp.addChild(bm);
sp.blendMode = BlendMode.LAYER;
var tf:TextField = new TextField();
tf.y = 401;
tf.background = true;
tf.backgroundColor = 0x0;
tf.defaultTextFormat = new TextFormat('Arial', 12, 0xe00000, true, false, false);
tf.selectable = false;
tf.text = 'Erase';
addChild(tf);
addChild(sp);
tf.addEventListener(MouseEvent.CLICK, toggleErasing);
sp.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
sp.addEventListener(MouseEvent.ROLL_OUT, endDrawing);
sp.addEventListener(MouseEvent.MOUSE_UP, endDrawing);
}
private function toggleErasing(e:MouseEvent):void {
erasing = !erasing;
if (erasing) (e.currentTarget as TextField).text = 'Erasing...'; else (e.currentTarget as TextField).text = 'Erase';
e.stopPropagation();
}
private function startDrawing(e:MouseEvent):void {
if (drawing) return;
sh = new Shape();
sh.graphics.lineStyle(3, Math.floor(Math.random() * 0x100000), 1,true);
if (erasing) sh.blendMode = BlendMode.ERASE;
sh.graphics.moveTo(e.localX, e.localY);
addEventListener(MouseEvent.MOUSE_MOVE, trackMouse);
sp.addChild(sh);
drawing = true;
}
private function endDrawing(e:MouseEvent):void {
if (!drawing) return;
removeEventListener(MouseEvent.MOUSE_MOVE, trackMouse);
if (erasing) bd.draw(sh, null, null, BlendMode.ERASE);
else bd.draw(sh);
sp.removeChild(sh);
sh = null;
drawing = false;
}
private function trackMouse(e:MouseEvent):void {
sh.graphics.lineTo(e.localX, e.localY);
}
}
经我测试,应该按原样工作,只需更改背景 :)