A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: defining multiple objects with for loop?

  1. #1
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693

    defining multiple objects with for loop?

    is it possible to do something like the following?
    Code:
    for (var i=0; i<variableAmt; i++) {
    	var ["temp" + [i]] = new Object();
    }
    basically to create
    temp0, temp1, temp2, etc... until it reaches the variable amount?

    thanks in advance,

    ChaseNYC
    mmm signature

  2. #2
    Senior Member dlowe93's Avatar
    Join Date
    Aug 2001
    Location
    Stumptown
    Posts
    621
    Quote Originally Posted by ChaseNYC
    is it possible to do something like the following?
    Code:
    for (var i=0; i<variableAmt; i++) {
    	var ["temp" + [i]] = new Object();
    }
    basically to create
    temp0, temp1, temp2, etc... until it reaches the variable amount?

    thanks in advance,

    ChaseNYC
    Yup. I do it all the time.

    d.
    dlowe93

  3. #3
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693
    ummm would you care to enlighten me?
    mmm signature

  4. #4
    Flash Incompetent ChaseNYC's Avatar
    Join Date
    Jun 2002
    Location
    new york city
    Posts
    693
    double post... ****ty comp...
    mmm signature

  5. #5
    Senior Member dlowe93's Avatar
    Join Date
    Aug 2001
    Location
    Stumptown
    Posts
    621
    Well, it depends on what you are trying to do. You can create a series of objects in an array like this:

    Code:
    var myArray:Array = new Array();
    for (var i = 0 ; i < n , i++){
        myArray[i] = new Object();
        //put a bunch of variable stuff in the new object here;
    }
    Or you can create a series of movie clips, say like you were creating a dynamic menu;

    Code:
    var menu:MovieClip;
    for(var i = 0 ; i < n ; i++){
       var clip:MovieClip = menu.createEmptyMovieClip(i + "_submenu", i);
       //or alternatively you could attach a movie, or add a class here;
    }
    Ultimately, by saying something like:

    var ["temp" + [i]] = new Object();

    You are creating object that are named:

    temp1
    temp2
    temp3
    etc.

    You can later refer to them by name directly, or through a loop again. Personally, I usually put everything in to an array just so I have them all in one place.

    If you have a more specifici question, I'd be happy to try and answer in more detail.

    Hope this helps.

    d.
    dlowe93

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