A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [mx04] strange problem with for(i in array)

  1. #1
    Junior Member
    Join Date
    Oct 2007
    Posts
    14

    [mx04] strange problem with for(i in array)

    I wasted several hours yesterday while trying to change the depths of a couple of MovieClips stored in an array:
    Code:
    		for(i in bug)
    		{
    			bug[i].swapDepths(4000+i);
    			trace(i + " depth " + bug[i].getDepth());
    		}
    As I hadn't used swapDepths() before, I assumed I was doing something wrong with it. But nothing seemed to work - their original depths remained unchanged. Then, just in case, I changed to a normal for loop:
    Code:
    		for(i=0; i<no_bugs; i++)
    		{
    			bug[i].swapDepths(4000+i);
    			trace(i + " depth " + bug[i].getDepth());
    		}
    And suddenly everything works like expected... Can someone please explain this?

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,876
    welcome to FK

    In the second loop i is a number so when you do 4000+i it makes sense...

    In the first loop i is a string so you are adding a string to 4000, which does'nt give you a valid depth...
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    Junior Member
    Join Date
    Oct 2007
    Posts
    14
    Quote Originally Posted by silentweed
    welcome to FK

    In the second loop i is a number so when you do 4000+i it makes sense...

    In the first loop i is a string so you are adding a string to 4000, which does'nt give you a valid depth...
    Aha, that makes sense - the for-in loop is really meant to be used for MovieClip properties. Thanks!

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