PDA

View Full Version : Array as string trouble



gforce7
October 21st, 2008, 11:58 AM
I had the following array of movie clips:

var photoFill:Array = [card0_mc.photoFill_mc, card1_mc.photoFill_mc, card2_mc.photoFill_mc, card3_mc.photoFill_mc]

with trace(photoFill); I get:
[object MovieClip],[object MovieClip],[object MovieClip],[object MovieClip]


what I want to do is build the Array through a loop based on the length of another Array like this:

var photoFill:Array = new Array
for(var n:int = 0; n < cardDensity.length; n++){
photoFill.push("card"+n+"_mc.photoFill_mc");
}
but now with trace(photoFill); I get:
card0_mc.photoFill_mc,card1_mc.photoFill_mc,card2_ mc.photoFill_mc,card3_mc.photoFill_mc

It's turned it into a string which is giving me an error when I try to reference photoFill[i].height in another loop. The Error is:
ReferenceError: Error #1056: Cannot create property height on String.

How can I turn the string back into objects?
Thanks

scottc
October 21st, 2008, 12:18 PM
try something like this?


var classRef:Class = getDefinitionByName("MyClassName") as Class;
var myObject:Object = new classRef();

EDIT:
or dont store them as string in the first place?

gforce7
October 21st, 2008, 01:33 PM
try something like this?


var classRef:Class = getDefinitionByName("MyClassName") as Class;
var myObject:Object = new classRef();EDIT:
or dont store them as string in the first place?

how do I not store them as a string? I don't want them as a string, but how do I push the clips into the array without making them strings?

clot
October 31st, 2008, 01:47 AM
I'm wondering as well actually. Funny how this works. Except I'd like to create a new object using my string name and as3 is having all kinds of trouble with it. In AS2 I would just create something like this in my for loop:

["mymc"+i].createEmptyMovieClip (or whatever).

Is there something equal to eval that has to be used for as3 just to use a string when creating a new dynamic object?

Valaran
October 31st, 2008, 12:15 PM
I'm gonna suggest you add the movieclips through actionscript rather then preplacing them, should make it easier to push them into the array:


var photoFill:Array = [];
var nlen:uint = cardDensity.length;
for(var n:uint = 0; n < nlen; n++){
// Note that the WhateverClass is whatever you named your class in the library
var tmpClip:WhateverClass = new WhateverClass();
photoFill.push(tmpClip);
this.addChild(tmpClip);
};


Also, I'm not sure, but shouldn't you be able to do something eval like just by using this["card" + i + "whatever.."], I know this was pretty much the same as eval, but I haven't really tried this in AS3 before. Another option for getting the clips into an array (if they are placed on the stage already) is to loop through all the children of the stage and add them to the array if they are of a particular type.

Hope that helped.