flash language_api_loops
Loops
ActionScript 3 introduces a new loop which allows you to iterate over an object’s properties and to directly access content from them.
In the following code we display the person object properties with the help of a for in loop:
var person:Object = { name : "Bobby", age : 50 }; for (var p:String in person) { /* display : age name */ trace( p ); }
Beware, the enumeration property order can change between machines. It is therefore essential not to count on this. For example the for in loop in ActionScript 3 doesn’t loop anymore from the last entry to the first as it did in ActionScript 1 and 2, but from the first to the last.
We can therefore use the for in loop in order to iterate within a array without worrying about the fact that the loop goes from the end of the array:
var arrayData:Array = [ 5654, 95, 54, 687968, 97851]; for ( var p:String in arrayData ) { /* display : 5654 95 54 687968 97851 */ trace( arrayData[p] ); }
The for each loop directly reaches the content of each property:
var person:Object = { name : "Bobby", age : 50 }; for each ( var p:* in person ) { /* display : 50 Bobby */ trace( p ); }
We can therefore no longer simply loop within the array with the help of the new for each loop:
var arrayData:Array = [ 5654, 95, 54, 687968, 97851 ]; for each ( var p:* in arrayData ) { /* display : 5654 95 54 687968 97851 */ trace( p ); }
We are now going to get into ActionScript 3’s garbage collector.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


