A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Dead Quick Question...

Hybrid View

  1. #1
    Senior Member
    Join Date
    Jul 2004
    Posts
    264

    Dead Quick Question...

    This will be really easy for most of you im sure:


    I've got a few variables named like this:

    Person1name
    person2name
    person3name

    and

    name1
    name2
    name3


    How do i call each one using the same loop?

    eg.

    n=0
    while(n < 6){

    Person("n")name = "hi";

    n=n+1
    }


    Thanks very much for this, its been driving me crazy!!!

    cheers/

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    You can use an array-style notation to refer to variables whose names are constructed dynamically.

    Try this:

    code:

    for (i = 1; i <= 3; ++i)
    {
    varname = "Person" + i + "name";
    varval = this[varname];
    trace(varval);

    varname = "name" + i;
    varval = this[varname];
    trace(varval);
    }



    If this[varname] doesn't work, replace it with _root[varname] (it depends on how your script is setup).

    This technique is somewhat cumbersome however. If it were me, I'd store the variables in an array, like so:

    code:

    people = ["Charley", "Fred", "Wilma"];

    for (i = 0; i < people.length; ++i)
    {
    varval = people[i];
    trace(varval);
    }


  3. #3
    Senior Member
    Join Date
    Jul 2004
    Posts
    264
    That only seems to work for text..

    I.e I can use arrays as peoples names,

    But it wont move a movieclip using arrays.

    e.g

    MClips = ("clip1", "clip2", "clip3");

    _root.MClips[2]._y = 300;



    please help.

    Thanks.

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    It's only working for strings because of the quotes. If they're movieclips, remove the quotes.

    Also, when specifying arrays like this, use square brackets, not parentheses.

    code:

    MClips = [clip1, clip2, clip3];
    _root.MClips[2]._y = 300;


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