如何屏蔽动态创建的MovieClip

时间:2011-08-04 13:30:36

标签: actionscript-3

我想要泡泡效果。我在freeactionscript.com的帮助下做了最大的努力。

现在,我想显示特定区域的动画。我使用了以下代码,但这不起作用。我怎么能这样做?

for (var i:uint = 0; i < noOfBubbles; i++) {
    var bubble:Bubble = new Bubble();
        bubbles.push(bubble);
        Layer_mc.mask = bubble;
        //i have used ENTER_FRAME handler for animation        
}

4 个答案:

答案 0 :(得分:1)

为什么不在Bubble课程中设置面具,以下是一个例子:

Main.as(文档类):

package 
{
    import com.flashdevelopprojects.display.Bubble;
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;

    [SWF(width="250", height="250", backgroundColor="0xFFFFFF", frameRate="32")]
    public class Main extends Sprite 
    {
        [Embed(source="assets/jellyfish.jpg")]
        private var JellyfishImage:Class;


        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var jellyfishImage:Bitmap = Bitmap(new JellyfishImage());

            var bubble:Bubble = new Bubble();
            bubble.addChild(jellyfishImage);

            addChild(bubble);

        }// end function

    }// end class

}// end package

Bubble.as:

package com.flashdevelopprojects.display 
{
    import flash.display.Bitmap;
    import flash.display.DisplayObject;
    import flash.display.Shape;
    import flash.display.Sprite;

    public class Bubble extends Sprite
    {
        [Embed(source="../assets/bubble.jpg")]
        private var BubbleImage:Class;

        private var _bubbleImage:Bitmap;
        private var _mask:Shape;
        private var _content:Sprite;

        public function Bubble() 
        {
            init();

        }// end function

        private function init():void
        {
            _content = new Sprite();
            super.addChild(_content);

            _bubbleImage = Bitmap(new BubbleImage());
            _bubbleImage.alpha = 0.5;
            super.addChild(_bubbleImage);

            _mask = new Shape();
            _mask.graphics.beginFill(0x000000);
            _mask.graphics.drawCircle(125, 125, 125);
            _mask.graphics.endFill();
            super.addChild(_mask);

            mask = _mask;

        }// end function

        override public function addChild(child:DisplayObject):DisplayObject 
        {
            child.width = 250;
            child.height = 250;
            child.alpha = 0.5;
            return _content.addChild(child);

        }// end function

    }// end class

}// end package

以下是运行Flash应用程序的图像:

enter image description here

答案 1 :(得分:0)

我想您可能已经忘记了在舞台(或其他容器)中添加气泡 - stage.addChild(bubble);,然后将其设置为另一个动画片段的掩码。

答案 2 :(得分:0)

使用蒙版时,建议在设置蒙版之前将蒙版和蒙版设置为缓存为位图:

bubble.cacheAsBitmap = true;
Layer_mc.cacheAsBitmap = true;
Layer_mc.mask = bubble;

答案 3 :(得分:0)

下面的代码只会添加一个气泡(最后一个)作为遮罩。

for (var i:uint = 0; i < noOfBubbles; i++) {
    var bubble:Bubble = new Bubble();
    bubbles.push(bubble);
    Layer_mc.mask = bubble;
    //i have used ENTER_FRAME handler for animation        
}

您需要在容器中添加气泡,并将其用作遮罩。

var theMask = new Sprite();
for (var i:uint = 0; i < noOfBubbles; i++) {
    var bubble:Bubble = new Bubble();
    theMask.addChild(bubble);
    bubbles.push(bubble);
    //i have used ENTER_FRAME handler for animation        
}
Layer_mc.mask = theMask;