Actionscript 3 AS3:如何清除特定像素/区域中的图形
我知道您使用的是Actionscript 3 AS3:如何清除特定像素/区域中的图形,actionscript-3,graphics,actionscript,Actionscript 3,Graphics,Actionscript,我知道您使用的是图形。清除要清除所有图形,但这会从后台清除图形,我想清除特定像素中的图形或x-y值之间的图形,我该怎么做?使用图形无法做到这一点。我刚试过,画透明的形状不会产生洞,唉 您应该将图形转换为位图实例并使用像素: package { import flash.geom.Matrix; import flash.geom.Rectangle; import flash.display.Sprite; import flash.display.Bitmap
图形。清除要清除所有图形,但这会从后台清除图形,我想清除特定像素中的图形或x-y值之间的图形,我该怎么做?使用图形无法做到这一点。我刚试过,画透明的形状不会产生洞,唉
您应该将图形转换为位图实例并使用像素:
package
{
import flash.geom.Matrix;
import flash.geom.Rectangle;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
public class Holey extends Sprite
{
public function Holey()
{
super();
// Lets create some example graphics.
graphics.beginFill(0x990000);
graphics.drawCircle(200, 200, 100);
graphics.endFill();
// Convert into raster and make 1 pixel transparent.
var aBit:Bitmap = rasterize(this);
aBit.bitmapData.setPixel32(50, 50, 0x00000000);
graphics.clear();
addChild(aBit);
}
private function rasterize(source:DisplayObject):Bitmap
{
// Obtain bounds of the graphics.
var aBounds:Rectangle = source.getBounds(source);
// Create raster of appropriate size.
var aRaster:BitmapData = new BitmapData(aBounds.width, aBounds.height, true, 0x00000000);
// Make an offset to capture all the graphics.
var aMatrix:Matrix = new Matrix;
aMatrix.translate(-aBounds.left, -aBounds.top);
aRaster.draw(source, aMatrix);
return new Bitmap(aRaster);
}
}
}
这样做的方法是。使用alpha遮罩(遮罩和遮罩都使用cacheAsBitmap=true
),您可以在遮罩上绘制透明像素以擦除零件。基本方法是:
将您希望受遮罩影响的所有图形放在一个公共容器中(如果您的意思是要剪切所有图形,那么它们已经在一个公共容器中:stage
或root
时间线。)
绘制一个位图数据对象,该对象在要擦除的区域中有一个透明的“孔”。例如:
// fill the stage with a solid rectangle
var maskBitmapData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0xff000000);
// erase part of it by drawing transparent pixels
maskBitmapData.fillRect(new Rectangle(20, 20, 200, 100), 0);
// create the mask object
var maskBitmap:Bitmap = new Bitmap(maskBitmapData);
maskBitmap.cacheAsBitmap = true; // this makes the mask an alpha mask
addChild(maskBitmap);
设置容器的名称。例如,要遮罩整个主时间线,请执行以下操作:
root.cacheAsBitmap = true; // this makes the mask an alpha mask
root.mask = maskBitmap;
打开堆栈溢出回答一些问题,思考下一个小时奶酪中的孔是如何放置的…:)
还可以将孔对象的属性设置为与cacheAsBitmap
结合使用。这与面具的工作原理类似,不同的是你实际上是在画整体,而不是它们外面的区域。
以下是一个例子:
//make cheese
var cheese:Sprite = new Sprite();
cheese.cacheAsBitmap = true;
stage.addChild(cheese);
cheese.x = cheese.y = 10;
//define holes
var holes:Shape = new Shape();
holes.blendMode = BlendMode.ERASE;
cheese.addChild(holes);
//draw cheese
var g = cheese.graphics;
g.beginFill(0xFFCC00);
g.drawRect(0,0,200,150);
//**Attack chees with mices.
g = holes.graphics;
for (var i:int = 0; i < 10; i++){
g.beginFill(0xFF00FF);
var hx = Math.random()*(cheese.width-7)+7;
var hy = Math.random()*(cheese.height-7)+7;
var s = Math.random()*15;
g.drawCircle(hx, hy, s);
g.endFill();
}
//做奶酪
var奶酪:雪碧=新雪碧();
cheese.cacheAsBitmap=true;
阶段:addChild(奶酪);
奶酪。x=奶酪。y=10;
//定义孔
变量孔:形状=新形状();
holes.blendMode=blendMode.ERASE;
奶酪。添加儿童(洞);
//画奶酪
var g=奶酪图形;
g、 beginll(0xFFCC00);
g、 drawRect(0,020150);
//**用老鼠攻击奶酪。
g=孔。图形;
对于(变量i:int=0;i<10;i++){
g、 Beginull(0xFF00FF);
var hx=Math.random()*(cheese.width-7)+7;
var hy=Math.random()*(cheese.height-7)+7;
var s=Math.random()*15;
g、 拉丝圈(hx、hy、s);
g、 endFill();
}
结果会是这样的:
编辑:
事实证明,如果将父对象的混合模式设置为层
(doc说应该自动设置),则不需要使用cacheAsBitmap
因此,您可以使用cheese.blendMode=blendMode.LAYER
而不是cheese.cacheAsBitmap=true代码>。如果我没记错的话,遮罩也不需要cahceAsBitmap
,即使在正常的混合模式下也是如此。图形只是数字(用于矢量/形状点)。只能清除位图中的像素。使用draw
在图形(向量)的容器(sprite/MC)上获得位图快照,然后您可以简单地使用setPixel
设置“清除”颜色像素。这个想法非常棒!不过,唯一让我担心的是,它会在舞台上还是只在同一个DisplayObjectContainer中的底层同级中切割孔?它只在容器中切割孔。还要注意,您的孔
对象必须是此容器中的最后一个子对象