Skip to main content
May 8, 2006
Question

String to MovieClip data type conversion

  • May 8, 2006
  • 1 reply
  • 416 views
Is there an easy way to convert a string into a movieClip reference?

I’m populating an array with movie clip references and other properties and then saving them to the sharedObject(). The problem is that many of the movieClips are nested within others and so the target path is not so simple. The only way I know of is with the _root[] method.

So...

_level0.instance1.instance2.instance3

Becomes...

_level0[instance1][instance2][instance3]

So in order to convert a sting generated by movieClip._target back into a movieClip reference all I can think of is to split it into an array and then reconstruct it like...


function stringToClip(clipString:String) {

mcpathArray = clipString.split("/");

for(var i:Number = 1; i<mcpathArray.length; ++i){
var tVar:String = "path"+i;
this[tVar] = mcpathArray ;
}

var mcName:MovieClip = new MovieClip();
switch (mcpathArray.length) {
case 2 :
trace("path1");
mcName = _root[path1];
return(mcName);
break;
case 3 :
trace("path2");
mcName = _root[path1][path2];
return(mcName);
break;
case 4 :
trace("path3");
mcName = _root[path1][path2][path3];
return(mcName);
break;
}

};


I know the last part could be delt with more elegantly, but the whole thing seams to way to much work to do such a simple task.

Thanks for any help with this,
Joe
This topic has been closed for replies.

1 reply

Inspiring
May 8, 2006
tevadar,

> Is there an easy way to convert a string into a movieClip
> reference?

There is. Both the eval() function and the array access operator will
do it for you.

See details here.

http://www.quip.net/blog/2006/flash/actionscript-20/reference-objects-dynamically


David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."


May 9, 2006
David, thank you VERY much! I knew there had to be an easier way.

BTW I like your site... it's very informative

Cheers