|
-
Senior Member
referencing a string to an MC
I'm not really sure what to title my problem, but I think I can explain it clearly. I have a variable, let's say myMC. myMC = "tree1.loc1"; Now if I do this
trace(tree1.loc1); the output window gives me this "_level0.tree1.loc1"
however if I do this:
trace(this[myMC]); the output window gives me this "undefined".
What am I doing wrong?
"If I have seen further it is by standing on the shoulders of giants." Isaac Newton
------------------------------------------------------------------------------
-
-
Senior Member
Are you looking to dynamically evaluate it?
If so I think you might be looking for:
_root[myMC]
Not quite sure why you would want to do that with a movieclip though. I have certainly done that sort of thing with strings to assign values to runtime created variables and with the function.apply method.
-
Senior Member
Also, where is this code going? Hard to tell what you are trying to do with 'this' when the code is out of context.
-
Senior Member
I want to reference a movieclip. I have an object and it has a property myLocation. myLocation will be equal to a path, such as "tree1.loc1" where tree1 is a movieclip and loc1 is a movieclip within tree1. I want to be able to take the value of myLocation and us that to change the properties of a movieclip.
and I tried the _root[myMC] and it didn't work either.
"If I have seen further it is by standing on the shoulders of giants." Isaac Newton
------------------------------------------------------------------------------
-
not sure if I understand it yet,- but how about this play?
PHP Code:
var mc = "my_movieClip";
var atr = "_rotation";
eval(mc)[atr] = 45;
it rotates the movieClip "my_movieClip" 45°
-
Feeling adventurous?
You have to use eval. Here's how things work: hey is a movieclip on the stage, ho is a movieclip in hey.
PHP Code:
myMc = "hey.ho";
my = "hey";
Mc = "ho";
trace(hey.ho);
trace(eval(myMc));
trace(this[my][Mc]);
Traces:
PHP Code:
_level0.hey.ho
_level0.hey.ho
_level0.hey.ho
I don't have a photograph, but you can have my footprints. They're upstairs in my socks.
-
Senior Member
thanks. I didn't know about the eval. It is exactly what I was looking for.
"If I have seen further it is by standing on the shoulders of giants." Isaac Newton
------------------------------------------------------------------------------
-
Senior Member
According to the eval() helpfile in flash 8:
"In Flash 5 or later, you cannot use eval() to dynamically set and retrieve the value of a variable or instance name, because you cannot useeval() on the left side of an equation."
If this is true, then the above would not work because you are using eval() on the left side of the equation. On the left side to evaluate something you would have to use array notation like the lost guru originally had this[] or _root[].
Does eval work on the left side of the equation again in AS3?
-
I think this should work without using eval:
PHP Code:
var myLoc:String = "mc1.mc2.mc3.mc4";
var myMCs:Array = myLoc.split('.');
var myMC = _root;
for(var i = 0; i < myMCs.length; i++) {
myMC = myMC[myMCs[i]];
}
trace(myMC);
I'm sure there's a shortcut to writing that as well.
Just an idea that came to me because I know eval is not supported in AS3.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|