# Can I use a variable inside addChild()?

**URL:** <https://forum.kirupa.com/t/can-i-use-a-variable-inside-addchild/300931>\
**Category:** flash\
**Created:** [November 30, 2009, 8:38pm UTC](https://forum.kirupa.com/t/can-i-use-a-variable-inside-addchild/300931 "2009-11-30T20:38:46Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![pillageTHENburn](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@pillageTHENburn](https://forum.kirupa.com/u/pillageTHENburn)\
**Post date:** [November 30, 2009, 8:38pm UTC](https://forum.kirupa.com/t/can-i-use-a-variable-inside-addchild/300931/1 "2009-11-30T20:38:46Z")

</div>

Here’s my (simplified) code:

```auto

var doorType:String = "DT0";
var openType:String = "OT0";
var imagePositioner:Sprite = new Sprite(); //Places a Sprite that will "contain" all the images.
this.addChildAt(imagePositioner,0);

var OT1_aluminum_exit:DisplayObject = new ot1_aluminum_exit();
var OT1_wood_exit:DisplayObject = new ot1_wood_exit();
var OT1_glass_exit:DisplayObject = new ot1_glass_exit();
var OT1_metal_exit:DisplayObject = new ot1_metal_exit();

var OT2_aluminum_exit:DisplayObject = new ot2_aluminum_exit();
var OT2_wood_exit:DisplayObject = new ot2_wood_exit();
var OT2_glass_exit:DisplayObject = new ot2_glass_exit();
var OT2_metal_exit:DisplayObject = new ot2_metal_exit();

//DT1, 2, 3 and 4 are radio buttons
DT1.addEventListener(MouseEvent.CLICK, doorTypeHandler);
DT2.addEventListener(MouseEvent.CLICK, doorTypeHandler);
DT3.addEventListener(MouseEvent.CLICK, doorTypeHandler);
DT4.addEventListener(MouseEvent.CLICK, doorTypeHandler);
function doorTypeHandler(event:MouseEvent):void {
    doorType = event.target.value; //re-assigns a value to doorType
    if (imagePositioner.numChildren >= 1) {
        imagePositioner.removeChildAt(0);
        } // Clears the imagePositioner so images don't stack
    if (doorType == "DT1") {
        imagePositioner.addChild(OT1_aluminum_exit);
            } else if (doorType == "DT2") {
            imagePositioner.addChild(OT1_wood_exit);
                } else if (doorType == "DT3") {
                imagePositioner.addChild(OT1_metal_exit);
                    } else if (doorType == "DT4") {
                    imagePositioner.addChild(OT1_glass_exit);
                    }
}

```

I am effectively trying to replace “imagePositioner.addChild(OT1\_aluminum\_exit);” with something like “imagePositioner.addChild(the current value of openType\_aluminum\_exit);”.

openType is defined on a previous screen, it will be equal to OT1 or OT2. I have two sets of images (movie clips) that each have a prefix of “ot1\_” or “ot2\_” (in their class name). Other than those prefixes the two sets of image names are the same. I think I could cut the amount of code in half if I could use a variable when I use addChild.

I am thinking the solution to this is simple but I am not very knowledgeable of AS3 and everything I’ve tried has failed. My theory is that addChild() needs to contain a movieclip, so putting a variable name in addChild() doesn’t work. I can’t seem to figure out how to do it!

Please let me know if I need to clarify.  
Thanks!
