A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: referencing a string to an MC

  1. #1
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784

    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
    ------------------------------------------------------------------------------

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    what do you want?

  3. #3
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    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.

  4. #4
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Also, where is this code going? Hard to tell what you are trying to do with 'this' when the code is out of context.

  5. #5
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784
    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
    ------------------------------------------------------------------------------

  6. #6
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    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°

  7. #7
    Feeling adventurous? T1ger's Avatar
    Join Date
    Mar 2004
    Posts
    850
    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.

  8. #8
    Senior Member TheLostGuru's Avatar
    Join Date
    Aug 2004
    Location
    I live on this webpage...
    Posts
    784
    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
    ------------------------------------------------------------------------------

  9. #9
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    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?

  10. #10
    Senior Member
    Join Date
    Jan 2006
    Location
    USA
    Posts
    383
    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 
    0myMCs.lengthi++) {
       
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center