Copy Children From One Flex Container to Another

July 16th, 2008 § 6 comments

Okay, this shouldn't be so difficult but I'm having a helluva time with it. I have two canvases. All I want to have happen is when I add a child to canvas A I want it to also be added to canvas B at the same time. Sounds easy right, but for some reason it's only showing up in the second canvas, not the first. It's driving me crazy. Any ideas out there?

var imageContainer:WallItem = new WallItem();
...
WallModel.getInstance().dropArea.addChild(imageContainer);
WallModel.getInstance().navCanvas.addChild(imageContainer);

When I do this the item only shows up in the "navCanvas" but if I take out the second addChild it shows up fine in "dropArea." What am I missing?

§ 6 Responses to Copy Children From One Flex Container to Another"

  • Logan says:

    A single instance of a component can only have a single parent and live in a single container. You are adding the same instance of the component to both containers, so when it is added to the second container, it is removed from the first.

    You need to instantiate 2 different instances of the component and configure them the same (bind them to the same data source, or bind them to each other) or wire up their events so they’ll both be handled the same, depending on what you’re trying to do.

  • tt says:

    thanks logan. that was very helpful, i got it working now.

  • james says:

    how about if the object I am going to copy is a UI component, a label for example. how can I copy it?

    I already tried using this..but doesn’t work

    registerClassAlias(“flash.display.DisplayObject”, DisplayObject );
    var tmp: DisplayObject;
    tmp = DisplayObject(ObjectUtil.copy(tempParent.getChildAt(0))); //this is the label

  • timT says:

    James, Logan’s advice still holds true. You will need to create another instance of the label object and bind the text to your original label.

  • james says:

    hi tim, can you show me a simple code? I want to copy the whole label not just the label text, but the whole properties including the size and positioning. The label I want to copy is in the first canvas of my viewstack and i want it to be copied on the 2nd canvas. tnx!

  • timT says:

    @James, the way I ended up having to do it was to create all the components at once, and add them to the various containers. So for example, if I needed multiple labels it would look something like the following:

    var label1:Label = new Label();
    var label2:Label = new Label();

    label1.x = 100;
    label1.y = 200;
    [...]

    label2.x = 100;
    label2.y = 200;
    [...]

    canvas1.addChild(label1);
    canvas2.addChild(label2);

    It’s a little clunky but it got the job done. Hope that helps you.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

What's this?

You are currently reading Copy Children From One Flex Container to Another at T3B.

meta