Flash Actionscript: dynamically named array accessing

Discussion in 'Web Design & Coding' started by NerdUprising, Jul 14, 2003.

  1. NerdUprising

    NerdUprising [ Method ]

    was wondering if anyone could help me with this interesting problem:

    in this script: (not taken out of context...rewrote to be simplified)

    countCurrent = 1;
    Info2 = new Array(I, need, help);
    FinalVar = ["Info" + countCurrent][0];

    trying to dynamically access the array from which the info will be pulled, but when this script runs... the first set of square brackets is all that is returned (FinalVar = "Info1") rather than returning "I" (the data in place 0 in the array)... It would be simple enough to simply name the array to be accessed... but this is running inside of a function that is written to repeat itself for every array (countCurrent is the number) rather than listing them all out, this allows for more arrays to be added or removed from the movie, all accessed within one function (save some time and filesize)

    anyone know how (or if) I could perhaps circumlocute this, or just get it to work at all? (my apologies if you couldnt understand the crap I just wrote...lol... can give more info if needed) Much thanks
  2. Zedric

    Zedric NTFS Guru Folding Team

    Isn't it better to use a multi-dimensional array (matrix) in this case? I don't know how they are represented in Flash but it might be something like this:

    Info = new Array();
    // Preferrably looped:
    Info[0] = new Array();
    Info[n] = new Array();

    Info[0][0] = "Whatever";
  3. NerdUprising

    NerdUprising [ Method ]

    after a little playing around... I have discovered the solution to my own problem (how nifty!)

    when using any dynamically named object (variable, array, movie clip, etc) Flash needs something to throw it off course...intentionally.

    when the array access operators are used, flash returns it without even thinking twice... so to get flash to read the operators as a name, there has to be "_root" before it (or _level0, if _root is being tempermental):

    _root["array"+countTotal][0] (note the lack of period between root and the array access operator...)

    and with this way, you can name anything heiarchially (is that a word?), so long as the last period is dropped:

    _root.newMC.textMC["variable"+countTotal] = 45