AS3:如何清除特定像素/区域中的图形

phe*_*pix 4 graphics actionscript actionscript-3 clear

我知道你graphics.clear用来清除所有图形,但是清除了舞台上的图形,我想清除特定像素中的图形或xy值之间的图像我该怎么做?

Org*_*nis 7

图形没有办法做到这一点.我刚试过,绘制透明的形状不会创造漏洞,唉.

您应该将您拥有的图形转换为Bitmap实例并使用像素:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Aar*_*all 5

这样做的方法是使用mask.使用alpha蒙版(使用蒙版和maskee cacheAsBitmap=true),您可以在蒙版上绘制透明像素以擦除部分.基本方法是:

  1. 将您想要通过掩码影响的所有图形放在一个公共容器中(如果您想要切割所有内容,那么它们已经在一个公共容器中:stageroot时间轴.)

  2. 在要擦除的区域中绘制一个具有透明"孔"的位图数据对象.例如:

    // 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);
    
    Run Code Online (Sandbox Code Playgroud)
  3. 设置容器的.mask属性.例如,要屏蔽整个主时间轴:

    root.cacheAsBitmap = true; // this makes the mask an alpha mask
    root.mask = maskBitmap;
    
    Run Code Online (Sandbox Code Playgroud)