Search code examples
flashactionscript-3flash-cs4

Multiple custom Components and the display list


I am using several instances of the same custom component on the same SWF. The component is essentially just a textfield with special sizing requirements. I also have another "wrapper" component that deals with the info from the textfield components. I was initially planning on using the stage index to associate the data with the textfield component it came from. This became more complicated than I anticipated. When I trace from the wrapper i can't get the index of the textfield components. So I got it in the component code and sent it with the info to the wrapper via a custom event. The problem there is that i can't tell which component is which because they are indexed identically. I can also see the component shim as well as the textfield.I'm just really confused about how programmatic components work on the display list.

My main Question. Is there a better way to associate the info with the proper component? I guess that's my only question. Please point me in the right direction.

EDIT: I forgot to add that my components are required to be dragged on to stage. Thanks so much for the answer but it will only work for adding the components programmatically. Any help would be greatly appreciated. Even if it's just a link to a possible solution or info that will help me sort it out. Thanks again.

EDIT2: I created a dummy place-holder component that just draws a rectangle then at runtime replaces it with programmatically generated textfield component. So Thanks so much for everyone that helped me on my way to this solution. If I come up with something better I'll update this.


Solution

  • You could give each TextField a unique name, like so:

    for(var i:int = 0; i < n; i++)
    {
        var txt:TextField = new MyCustomTextField();
        txt.name = "txt_" + i.toString();
        stage.addChild(txt);
    }
    

    where for example txt[99] = stage.getChildByName("txt_" + 99);

    ... or you could use an Array or Vector to hold a reference to each TextField.