AS3 - 使用动态文本作为掩码

时间:2015-04-01 17:05:53

标签: actionscript-3 flash text mask

很确定这是可能的。我需要动态加载和填充文本字段,然后将该文本用作它下面的movieclip的掩码。我有

function doMask():void{
    myMc.dynamicTxtField.text = "some text to use as a mask";
    mcToBeMasked.mask = myMc;
}

doMask();

整个文本字段被用作掩码,而不仅仅是它包含的文本。

3 个答案:

答案 0 :(得分:0)

我不确定如何使用动态文本作为蒙版,但您可以使用混合模式实现类似的效果。例如将图像乘以黑色背景上的动态文本顶部。

答案 1 :(得分:0)

在设置遮罩之前将其添加到两者。

myMc.dynamicTxtField.cacheAsBitmap = true;
mcToBeMasked.cacheAsBitmap = true;

答案 2 :(得分:0)

这可以使用[BlendMode] [1]实现,如下所示(建议here):

    container.blendMode = BlendMode.LAYER;//parent of toMask and textfield
    toMask.blendMode = BlendMode.NORMAL;//sprite to be masked
    tf.blendMode = BlendMode.ALPHA;//masking textfield

a working example on wonderfl

package {
    import flash.text.TextFormat;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.display.BlendMode;
    import flash.events.MouseEvent;
    public class FlashTest extends Sprite {
        private var tf:TextField = new TextField();
        private var container:Sprite = new Sprite();
        private var toMask:Sprite = new Sprite();
        private const WIDTH:int = 250;
        private const HEIGHT:int = 150;
        public function FlashTest() {
            graphics.beginFill(0x0000ff);
            graphics.drawRect(0, 0, 300, 300);
            graphics.endFill();       

            addChild(container);     
            tf.defaultTextFormat = new TextFormat(null, 15, 0, true);
            tf.text = 'sample text';
            tf.cacheAsBitmap = true;
            container.blendMode = BlendMode.LAYER;
            toMask.blendMode = BlendMode.NORMAL;
            tf.blendMode = BlendMode.ALPHA;
            container.addChild(toMask);
            container.addChild(tf);
            toMask.graphics.beginFill(0x00ff00);
            toMask.graphics.drawRect(0, 0, WIDTH, HEIGHT);
            toMask.graphics.endFill();
            tf.width = WIDTH;
            tf.height = HEIGHT;
            stage.addEventListener(MouseEvent.CLICK, onClick);
        }

        private function onClick(e:MouseEvent = null):void{
            tf.text = 'sample text ' + Math.random();
        }
    }
}