开发者

How to mask dynamically created MovieClips

开发者 https://www.devze.com 2023-03-26 01:23 出处:网络
I am trying to get bubble effect. I have did the maximum with the help of freeactionscript.com. Now, i want to display the anim for the particular area. I used the following code, but that is not wo

I am trying to get bubble effect. I have did the maximum with the help of freeactionscript.com.

Now, i want to display the anim for the particular area. I used the following code, but that is not working. 开发者_如何转开发How can I do that?

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        
}


Why not set up the mask from within your Bubble class, the following is an example of this:

Main.as(document class):

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

Here is an image of the flash application running:

How to mask dynamically created MovieClips


I think you may have forgotten about adding bubble to your stage (or another container) - stage.addChild(bubble); and then set it to be a mask of another movieclip.


When working with masks it is advisable to set both the mask and the maskee to cache as bitmaps before setting up a mask:

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


The code below will only add one bubble (the last one) as the mask.

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        
}

You need to add bubbles into a container, and use that as the mask.

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消