A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Setting dynamic variables

  1. #1
    Member
    Join Date
    Apr 2004
    Location
    IA, USA
    Posts
    57

    Setting dynamic variables

    I have set up a "for" loop that I would like to have set the alpha for a number of variables. I know I can set a variable this way:

    Code:
    _root.treeListenerFront.treeListener1._alpha=0;
    And I know I can set a dynamic variable using an array:

    Code:
    for(i=1; i<=32; i++){
         _root["treeListenerFront.treeListener"+i]=0;
    }
    But I can't seem to use the second method to set the alpha value. For instance, the following isn't working for me:

    Code:
    _root["treeListenerFront.treeListener"+i+"._alpha"]=0;
    What is the best way to go about doing this?

    Thanks, in advance.

  2. #2
    Novemberain Master
    Join Date
    Sep 2004
    Location
    Void
    Posts
    390
    _root["treeListenerFront.treeListener"+i] gives you an object (associative array access). Square brackets are used for object's name evaluation. I hope this is clean.

    Then, to solve your problem you should correct your code this way:

    Code:
    this["myObjNumber" + i]._alpha = someAlpha;


    Never give up...

  3. #3
    Member
    Join Date
    Apr 2004
    Location
    IA, USA
    Posts
    57
    I'm not the most expert programmer, so I'm not sure I understand everything you've told me, but I tried using your example and it still isn't working.

    I have:

    Code:
    //_root.treeListenerFront.treeListener1._alpha=0;
    _root["treeListenerFront.treeListener"+i]._alpha=0;
    The commented code will deliver exactly what I want, but it's not dynamic. It seem sthat the un-commented code should work, but it doesn't do anything.
    Last edited by Rancor; 05-03-2005 at 04:21 PM.

  4. #4
    Member
    Join Date
    Apr 2004
    Location
    IA, USA
    Posts
    57
    I've narrowed the problem a little bit, I think.

    _root["treeListenerFront"]._alpha=0;

    will work, but it's when I add the additional ".treeListener" + i] that things stop working.

    So it must be something with this part of the code. Is the engine trying to add the number to the string, instead of attaching it?

    (I've set up another working example in another file that does what I want it to (minus the ._alpha)

    It is set up like this:
    _root["leafMG.leafGenerator.leafPlayed"+leafCount]=false;
    which is pretty similar, and this one works.)

    So anybody else have any thoughts?

  5. #5
    Novemberain Master
    Join Date
    Sep 2004
    Location
    Void
    Posts
    390
    _root.treeListenerFront["treeListener"+i]._alpha=0;


    Never give up...

  6. #6
    Member
    Join Date
    Apr 2004
    Location
    IA, USA
    Posts
    57
    Foolish!

    Thanks, man. It works now.

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