# Simplify code with for loop?

**URL:** <https://forum.kirupa.com/t/simplify-code-with-for-loop/330769>\
**Category:** flash\
**Created:** [March 13, 2013, 10:45am UTC](https://forum.kirupa.com/t/simplify-code-with-for-loop/330769 "2013-03-13T10:45:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![emmti](https://avatars.discourse-cdn.com/v4/letter/e/46a35a/32.png) [@emmti](https://forum.kirupa.com/u/emmti)\
**Post date:** [March 13, 2013, 10:45am UTC](https://forum.kirupa.com/t/simplify-code-with-for-loop/330769/1 "2013-03-13T10:45:02Z")

</div>

I have 3 diagrams with movieclips. In each diagram, when one movieclip is selected, that movieclip opens up in a container, using addChild. The movieclip var names reflect which diagram they are from, ie soc\_a1, soc\_b1, soc\_c1 belong to one diagram , fam\_a1, fam\_b1, fam\_c1 belong to the second diagram, and cross\_a1, cross\_a2, cross\_c1 belong to the third diagram. Each movieclip has 5-6 variations of the movieclip, named like this soc\_a1, soc\_a1\_zon, soc\_a1\_sem, soc\_a1\_tou, soc\_a1\_mov, which can each be revealed through different buttons. So here is my code for one of these button, ‘gr\_options.nor\_btn’ which will reveal the normal state of the movieclips (i.e. soc\_a1 or fam\_b1 etc), depending on which movieclip is currently shown in the container.

So my question is if anyone has suggestions to simplify my code? I have hundreds of movieclips to add, so this code is way to long. I think this should be done using a for loop, but I cannot figure it out as my var names contain both letters and numbers. I have also looked into Arrays, however I cannot seem to get them to work with movieclips loaded dynamically (without instance names).

Please let me know if further explanation is needed.

```auto

gr_options.nor_btn.addEventListener(MouseEvent.MOUSE_DOWN,view_greeting_normal);
function view_greeting_normal(event:MouseEvent):void {
	gr_options.greeting_views.text = "Vector Animation";
	if (container.contains(fam_c5_zon)){
		container.removeChildAt(0);
		container.addChild(fam_c5);
	}
	else if (container.contains(fam_c5_spa)){
		container.removeChildAt(0);
		container.addChild(fam_c5);
	}
	else if (container.contains(fam_c5_tou)){
		container.removeChildAt(0);
		container.addChild(fam_c5);
	}
	else if (container.contains(fam_c5_mov)){
		container.removeChildAt(0);
		container.addChild(fam_c5);
	}
	else if (container.contains(fam_d5_zon)){
		container.removeChildAt(0);
		container.addChild(fam_d5);
	}
	else if (container.contains(fam_d5_spa)){
		container.removeChildAt(0);
		container.addChild(fam_d5);
	}
	else if (container.contains(fam_d5_tou)){
		container.removeChildAt(0);
		container.addChild(fam_d5);
	}
	else if (container.contains(fam_d5_mov)){
		container.removeChildAt(0);
		container.addChild(fam_d5);
	}
	else if (container.contains(soc_b5_zon)){
		container.removeChildAt(0);
		container.addChild(soc_b5);
	}
	else if (container.contains(soc_b5_spa)){
		container.removeChildAt(0);
		container.addChild(soc_b5);
	}
	else if (container.contains(soc_b5_tou)){
		container.removeChildAt(0);
		container.addChild(soc_b5);
	}
	else if (container.contains(soc_b5_mov)){
		container.removeChildAt(0);
		container.addChild(soc_b5);
	}
	else if (container.contains(soc_e5_zon)){
		container.removeChildAt(0);
		container.addChild(soc_e5);
	}
	else if (container.contains(soc_e5_spa)){
		container.removeChildAt(0);
		container.addChild(soc_e5);
	}
	else if (container.contains(soc_e5_tou)){
		container.removeChildAt(0);
		container.addChild(soc_e5);
	}
	else if (container.contains(soc_e5_mov)){
		container.removeChildAt(0);
		container.addChild(soc_e5);
	}
	else if (container.contains(cross_c5_zon)){
		container.removeChildAt(0);
		container.addChild(cross_c5);
	}
	else if (container.contains(cross_c5_spa)){
		container.removeChildAt(0);
		container.addChild(cross_c5);
	}
	else if (container.contains(cross_c5_tou)){
		container.removeChildAt(0);
		container.addChild(cross_c5);
	}
	else if (container.contains(cross_c5_mov)){
		container.removeChildAt(0);
		container.addChild(cross_c5);
	}
}

```
