Search code examples
actionscript-3flashaddchild

addchild with multiple movieclip in as3


var txt_mc:movieClip=new movieClip();
    createTxt(3)
    function createTxt(_no):void
    {
        var _sy = 0;
        for (var i=0; i<_no; i++)
        {
            var txt:TextField = new TextField();
            txt_fmt.size = _size;
            txt.defaultTextFormat = txt_fmt;
            //txt.autoSize = TextFieldAutoSize.CENTER;
            txt.autoSize = TextFieldAutoSize.LEFT;
            txt.selectable = false;
            txt.embedFonts = true;
            txt.x = 0;
            txt.y = _sy;
            _sy = _sy + 25;
            //txt.border = true
            txt.text = "Enter your text here";
            txt_mc.addChild(txt);
        }
        mc1.addChild(txt_mc);
            mc2.addChild(txt_mc);
    }

How can i addchild with multiple movieclip. I was create a movieclip and wants to addchild in two movieclips which are located on stage. please help me out.

I want to txt_mc will be add in mc1 and mc2 and from that code txt_mc is add in mc2 only.


Solution

  • You can't add the same instance of one movie clip in two different containers. But you can create two similar txt_mc instances and add them to mc1 and mc2.

    UPDATE: So, you can modify your createTxt function, so it will return new instance of txt_mc each time you call it. And add it to every container you want:

    function createTxt(_no):MovieClip
    {
        var txt_mc:movieClip=new MovieClip();
        var _sy = 0;
        for (var i=0; i<_no; i++)
        {
            var txt:TextField = new TextField();
            txt_fmt.size = _size;
            txt.defaultTextFormat = txt_fmt;
            //txt.autoSize = TextFieldAutoSize.CENTER;
            txt.autoSize = TextFieldAutoSize.LEFT;
            txt.selectable = false;
            txt.embedFonts = true;
            txt.x = 0;
            txt.y = _sy;
            _sy = _sy + 25;
            //txt.border = true
            txt.text = "Enter your text here";
            txt_mc.addChild(txt);
        }
        return txt_mc;
    }
    
    mc1.addChild(createTxt(3));
    mc2.addChild(createTxt(3));