A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Simple question bout for loops

  1. #1
    Mind Chaos
    Join Date
    Oct 2006
    Posts
    96

    Simple question bout for loops

    Ok, so I have 3 objects, named "platform" and then a number, from 1 to 3 ish. On the stage they are platform_mc1 - 3.

    Using for loops to control them all at once is working fine as long as I set the array manually, but when I try to automate it a little more it runs into a problem...no idea why....

    So what I'm doing is this:

    Code:
    var platform:Array = []
    for (i=1; i<3; i++){
    	platform[i] = platform_mc+i
    }
    But it's giving me problems...I don't see why. Here's the way I see it...

    -Declare array named platform, set it to empty.

    -For as long as i, which starts at 1 and increases by one each time, is less than 3, then concatenate the prefix (platform_mc) to an increasing number (thus being platform_mc1 and so forth), and throw it in the list (platform) at the index of i.


    What's wrong here??


    Also the error is : Access of undefined property platform_mc

    Thanks
    Last edited by Sy Mercade; 11-11-2011 at 08:19 PM.
    [Don Kanonji]
    *crosses arms* WAAAAAAAAHAHAHAHAAHAHAHA!!!!!
    [/Don Kanonji]

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    What's wrong is that platform_mc is not a prefix. In that code, it is an unknown identifier. You can create a String by concatenating a String prefix and a number, but then that is still a String, and you'll need to get the actual object with that name using getChildByName.
    You have also not declared i, and with the loop as you have it i will only range from 1 to 2, not 3 because 3 is not < 3. Also, by setting platform[i], you'll have an empty slot at platform[0] because you haven't put anything in there. Use push instead of setting the array index directly.

    Code:
    var platforms:Array = [];
    for (var i:int = 1; i <=3; i++){
      platforms.push(getChildByName("platform_mc"+i);
    }
    Now you will have an array platforms with your three platform_mc instances in it at indices 0, 1, and 2.

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