Retrieve array index as addChild parameter


#1

Hello everybody, hopefully somebody will be able to help me.

I have an array with some indexes inside and I want to pass one of those index as a addChild parameter. If I type it (i.e. Tambor1_mc.addChild(atunIcon); then it´s fine, I can see the mc on the stage. But if I try to pass it dynamically, I get a #1034 error (can´t convert “tunaIcon” in flash.display.DisplayObject.

If I try Tambor1_mc.addChild(arrayTambores.bigArrayTambor1[0] as DisplayObject); I get a #2007 error (yhe value of the child parameter must be different from null).

Any ideas? Thanks in advance!

I have this in Main.as:

package 
{
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;

	{
		public class Main extends MovieClip
		{
			public function Main():void
			{
				var Tambor1_mc:MovieClip = new MovieClip();

				var atunIcon:atun = new atun();
				var cafeIcon:cafe = new cafe();


				// BUTTON
				makeButton(my_mc, my_mc_click);

				function my_mc_click(evt: MouseEvent):void
				{					
					
					Tambor1_mc.addChild(arrayTambores.bigArrayTambor1[0]);
					stage.addChild(Tambor1_mc);
					
				}


				function makeButton(which_mc: MovieClip, clickFunction: Function):void
				{
					which_mc.buttonMode = true;
					which_mc.useHandCursor = true;
					which_mc.mouseChildren = false;
					which_mc.addEventListener(MouseEvent.CLICK, clickFunction);
				}
			}
		}
	}	
};

In arrayTambores.as, I have:

package 
{

	public class arrayTambores
	{

		public static var tempArray: Array = new Array();
		public static var bigArrayTambor1: Array = new Array();
		public static var bigArrayTambor2: Array = new Array();
		public static var bigArrayTambor3: Array = new Array();
		public static var randomPos:Number = 0;
		public static var a:int = 0;
		public static var z:int = 0;

		

		for (a = 0; a < 13; a++)
		{
			tempArray.unshift("atunIcon");
		}

		for (a = 0; a < 13; a++)
		{
			tempArray.unshift("cafeIcon");
		}

		public function arrayTambores()
		{

		}
	}
}

(The code is larger than what I paste here, but the rest of it seems to work fine, so I didn´t want to get messy here)


#2

You can’t use addChild with numbers, which is what I assume you mean by “indexes”? You need to add display object instances. The fact that the as operator results in null shows that its not the right type.

You’re not showing what’s actually in bigArrayTambor1, so I can’t tell whats going wrong there. What are you putting in bigArrayTambor1?

Edit: oh wait, I think the “tunaIcon” error is the key. “tunaIcon” is a string, not a display object. You can’t add strings either. You need to have display objects. Otherwise you need to have a separate operation to read the string, see what it says, and create the correct display object based on that string so that can instead be added (rather than the string itself).


#3

Thank you very much for your kind answer, Senocular, I´ll try to follow your valuable guides.

Best regards