;

PDA

Click to See Complete Forum and Search --> : Adding a movieclip from the stage into an array?


Vathian
05-06-2009, 10:32 PM
Hello and good day to you!

I am attempting to push an object into an array based on its instance name on the stage...how do I do this?

On my stage I've simply made a box, turned it into a movie clip named "mcBox" and have given it an instance name of "box_mc" on the stage. Here is my current test code:



var energy:Number = 0.75;
var boxArray:Array = new Array;

box_mc.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
if(energy >= 0.5)
{
boxArray.push(box_mc);
}

trace(boxArray);
}



Since the default amount of energy satisfies the if statement I push the movieClip into the array and then trace it. In my output I receive "[object MovieClip]"...is there anyway to distinguish it by its instance name?

What am I doing wrong?

Ralgoth
05-06-2009, 11:55 PM
you can assign the object a name (box_mc.name), but with AS3 objects are no longer retrieved by name. They are located by reference. Which is why you can have a loop to create multiple objects with the same "name" w/o overwriting the previous object

var my_array:Array = new Array();
var my_sprite:Sprite;
for(var i:uint=0; i<10; i++){
my_sprite = new Sprite();
my_sprite.name = "Sprite_"+i;
my_sprite.graphics.beginFill(0x00000);
my_sprite.graphics.drawRect(100,100,-50,-50);
my_sprite.graphics.endFill();
my_sprite.x = Math.random()*stage.stageWidth;
my_sprite.y = Math.random()*stage.stageHeight;
stage.addChild(my_sprite);
my_array[my_sprite.name] = my_sprite;
}

each of the above "my_sprite" objects coexist, and are referenced in my_array. So if you need to do something to "Sprite_5" you can say my_array["Sprite_5"].visible = false; and so forth

5TonsOfFlax
05-07-2009, 12:06 AM
You're actually doing everything right. Your clip is going into the array just fine. The problem is that the toString method of an Array simply calls toString on all the elements of that array, so you get the default trace output of a movieclip, which is "[object MovieClip]".

If your clip was an instance of a particular class, and that class had a different toString implementation, you'd get that output instead.

For your purposes, perhaps you could call this instead of trace on the array:


function traceArray(a:Array):void{
var totrace:String;
if (a != null){
totrace = "[";
}else{
trace("null!");
return;
}
for (var i:int = 0; i < a.length; i++){
if (a[i] is MovieClip){
totrace += a[i].name;
}else{
totrace += a[i].toString();
}
if (i < a.length -1){
totrace += ", ";
}
}
totrace += "]";
trace(totrace);
}


But that's really going out of your way.

Oh also, be aware that the same clip can go in the array multiple times because array contents do not have to be unique. With the code you posted, it would be in the array for each time you clicked it.

5TonsOfFlax
05-07-2009, 12:10 AM
Ralgoth, I would make a minor adjustment to your code even though it should work as posted:

var my_array:Array = new Array();
var my_sprite:Sprite;
for(var i:uint=0; i<10; i++){
my_sprite = new Sprite();
my_sprite.name = "Sprite_"+i;
my_sprite.graphics.beginFill(0x00000);
my_sprite.graphics.drawRect(100,100,-50,-50);
my_sprite.graphics.endFill();
my_sprite.x = Math.random()*stage.stageWidth;
my_sprite.y = Math.random()*stage.stageHeight;
stage.addChild(my_sprite);
my_array.push(my_sprite);
}


Then to reference the Sprite that would have the name "Sprite_5" (which is actually the 6th sprite), use

my_array[5];

Ralgoth
05-07-2009, 12:19 AM
I actually used push initially, but decided to change it to use the string so he can reference by name if he needs to... the loop wasn't a very good example for that though :)

5TonsOfFlax
05-07-2009, 12:23 AM
In that example, you can access by name using getChildByName

stage.getChildByName("Sprite_5");

Ralgoth
05-07-2009, 12:35 AM
this is true. It's one of those things that I never use, and so it gets filed away into that dusty cabinet somewhere in the back of my brain.

Ohwell, gg.

Ralgoth
05-07-2009, 08:29 AM
I just remembered why I used array["string"]. In a past project I had to build a 3D world for a holiday card. So to let an object enter inside a house, I had to make that object become a child of the house. Using the array helped me keep track of where all my object were.

Though, unless your display objects jump from parent to parent, this probably isn't necessary.

5TonsOfFlax
05-07-2009, 09:13 AM
I don't see how that follows, but that's not really relevant to the original question anyway.

For future reference, when you want a map from Strings to objects, you can use a plain old Object, or a Dictionary. Both of those are more suited to that than an Array.

Vathian
05-08-2009, 01:15 AM
Thanks very much guys!