Flash AS2.0
Creating Simple XML Object
var myXML:XML = new XML();
myXML.ignoreWhite=true;
myXML.load(“oman3d.xml”);
myXML.onLoad = function(success){
if (success){
trace (myXML);
}
see in detail
var myXML:XML = new XML();
myXML.ignoreWhite=true;
myXML.load(“oman3d.xml”);
myXML.onLoad = function(success) {
if (success) {
var myImage = myXML.firstChild.childNodes;
for (i=0; i<myImage.length; i++) {
var imageNumber = i+1;
var imageName = myImage[i].attributes.title;
var imageURL = myImage[i].firstChild.nodeValue;
trace (“My image number “+imageNumber+” is titled “+imageName+” and its URL is “+imageURL+”.”)
}
}
};
Creating Arrays
// Create a new array with zero elements.
var aEmployees:Array = new Array();
A single parameter specifying the number of elements
// Create a new array with four elements.
var aEmployees:Array = new Array(4);
A list of parameters, each of which is a new value to insert into a new element in
the array
// Create a new array with zero elements.
var aEmployees:Array = new Array(“Arun”, “Peter”, “Chris”,“Heather”);
Appending Values to the End of an Array
push()
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
aEmployees.push(“Ruth”);
trace(aEmployees.toString());
The result is:
Arun,Peter,Chris,Heather,Ruth
Prepending Elements to the Beginning of an Array
unshift()
The following is an example of the unshift() method:
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
aEmployees.unshift(“Ruth”, “Hao”, “Laura”);
trace(aEmployees.toString());
And this is the resulting output:
Ruth,Hao,Laura,Arun,Peter,Chris,Heather
Inserting Elements into an Array
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
aEmployees.splice(3, 0, “Ruth”, “Hao”, “Laura”);
trace(aEmployees.toString());
The preceding code will display the following in the Output panel:
Arun,Peter,Chris,Ruth,Hao,Laura,Heather
Removing Elements from an Array
pop()
The following is an example of how to use the pop() method:
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
var sAnEmployee:String = String(aEmployees.pop());
trace(aEmployees.toString());
trace(sAnEmployee);
The Output panel will display the following:
Arun,Peter,Chris
Removing the First Element of an Array
The following is an example of the shift() method:
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
var sAnEmployee:String = String(aEmployees.shift());
trace(aEmployees.toString());
trace(sAnEmployee);
The Output panel will display the following:
Peter,Chris,Heather
Removing Elements from Within an Array
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
var aRemovedEmployees:Array = aEmployees.splice(2, 2);
trace(aEmployees.toString());
trace(aRemovedEmployees.toString());
The preceding code will result in the following display in the Output panel:
Reading Data from Arrays
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
for(var i:Number = 0; i < aEmployees.length; i++) {
trace(aEmployees[i]);
}
The preceding example will display the values in the array one at a time in the Output panel
as follows:
Arun
Peter
Chris
Heather
Using Different Types of Arrays
Working with Single-Dimension Arrays
var aLetters:Array = [“a”, “b”, “c”];
var aNoLetters:Array = new Array();
var aMoreLetters:Array = new Array(“d”, “e”, “f”);
Working with Parallel Arrays
var aEmployees:Array = new Array();
aEmployees.push(“Arun:555-1234”);
aEmployees.push(“Peter:555-4321”);
aEmployees.push(“Chris:555-5678”);
aEmployees.push(“Heather:555-8765”);
var aTempEmployeeInfo:Array = null;
for(var i:Number = 0; i < aEmployees.length; i++) {
aTempEmployeeInfo = aEmployees[i].split(“:”);
trace(“Employee:” + aTempEmployeeInfo[0]);
trace(“Phone Number:” + aTempEmployeeInfo[1]);
}
The preceding will result in the following display in the Output panel:
Employee:Arun
Phone Number:555-1234
Employee:Peter
Phone Number:555-4321
Employee:Chris
Phone Number:555-5678
Employee:Heather
Phone Number:555-8765
Working with Multidimensional Arrays
var aEmployees:Array = new Array();
aEmployees.push([“Arun”, “555-1234”]);
aEmployees.push([“Peter”, “555-4321”]);
aEmployees.push([“Chris”, “555-5678”]);
aEmployees.push([“Heather”, “555-8765”]);
for(var i:Number = 0; i < aEmployees.length; i++) {
trace(“Employee:” + aEmployees[i][0]);
trace(“Phone Number:” + aEmployees[i][1]);
}
Working with Arrays of Objects
var aEmployees:Array = new Array();
aEmployees.push({employee:”Arun”, phone:”555-1234”});
aEmployees.push({employee:”Peter”, phone:”555-4321”});
aEmployees.push({employee:”Chris”, phone:”555-5678”});
aEmployees.push({employee:”Heather”, phone:”555-8765”});
for(var i:Number = 0; i < aEmployees.length; i++) {
trace(“Employee:” + aEmployees[i].employee);
trace(“Phone Number:” + aEmployees[i].phone);
}
Creating New Arrays from Existing Arrays
You can create a new array that contains the elements of several other arrays using the
concat() method. You invoke the method from an array and pass it parameters specifying
the other arrays whose elements you want to add to the new array. Flash then creates a new
array and adds all the elements of the original arrays to the new one. Here is an example:
var aEmployeesExec:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
var aEmployeesNew:Array = [“Gilberto”, “Mary”];
var aEmployeesStaff:Array = [“Ayla”, “Riad”];
var aEmployeesAll:Array = aEmployeesExec.concat(aEmployeesNew, aEmployeesStaff);
trace(aEmployeesAll.toString());
In the example, the Output panel will display the following:
Arun,Peter,Chris,Heather,Gilberto,Mary,Ayla,Riad
Extracting Subsets of Array Elements
var aEmployeesAll:Array = [“Arun”, “Peter”, “Chris”, “Heather”,
“Gilberto”, “Mary”, “Ayla”, “Riad”];
var aEmployeesExec:Array = aEmployeesAll.slice(0, 4);
var aEmployeesNew:Array = aEmployeesAll.slice(4, 6);
var aEmployeesStaff:Array = aEmployeesAll.slice(6);
trace(aEmployeesExe.toString());
trace(aEmployeesNew.toString());
trace(aEmployeesStaff.toString());
In this example the Output panel will display the following:
Arun,Peter,Chris,Heather
Gilberto,Mary
Ayla,Riad
Sorting Arrays
Sorting Simply
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
aEmployees.sort();
trace(aEmployees.toString());
Sorting More Complexly
Sorting Numerically
var aNumbers:Array = [10, 1, 2, 15, 21, 13, 33, 3];
aNumbers.sort();
trace(aNumbers.toString());
What you’ll see in the Output panel, should you try this, is the following:
1,10,13,15,2,21,3,33
Sorting in Descending Order
aEmployees.sort(Array.DESCENDING);
trace(aEmployees.toString());
This causes the array to sort in reverse alphabetical order. The following will be displayed in
the Output panel:
Peter,Heather,Chris,Arun
Sorting Regardless of Case
var aWords:Array = [“orange”, “Sedona”, “apple”, “Caracas”];
aWords.sort();
trace(aWords.toString());
In this case, the array will be sorted as follows:
Caracas,Sedona,apple,orange
Sorting and Testing for Unique Values
Here is an example of the sort() method with the Array.UNIQUESORT constant:
var aEmployees:Array = [“Arun”, “Peter”, “Chris”, “Heather”];
if(aEmployees.sort(Array.UNIQUESORT) != 0) {
trace(aEmployees.toString());
}
else {
trace(“Array has duplicate elements, and has not been sorted. “);
trace(aEmployees.toString());
}
The preceding code will display the sorted array because aEmployees has no duplicate
entries:
Arun,Chris,Heather,Peter
Sorting Arrays of Associative Arrays
function displayArray(aArray:Array) {
var sElement:String = null;
for(var i:Number = 0; i < aArray.length; i++) {
sElement = “”;
for(var key in aArray[i]) {
sElement += aArray[i][key] + “ “;
}
trace(sElement);
}
}
var aCars:Array = new Array();
aCars.push({make: “Oldsmobile”, model: “Alero”, extColor: “blue”});
aCars.push({make: “Honda”, model: “Accord”, extColor: “red”});
aCars.push({make: “Volvo”, model: “242”, extColor: “red”});
aCars.sortOn(“make”);
displayArray(aCars);
Then, you invoke the sortOn() method, telling it which key to sort on. The display in
the Output panel looks like this:
Honda Accord red
Oldsmobile Alero blue
Volvo 242 red
Sorting on Multiple Keys
function displayArray(aArray:Array) {
var sElement:String = null;
for(var i:Number = 0; i < aArray.length; i++) {
sElement = “”;
for(var key in aArray[i]) {
sElement += aArray[i][key] + “ “;
}
trace(sElement);
}
}
var aCars:Array = new Array();
aCars.push({make: “Oldsmobile”, model: “Alero”, extColor: “blue”});
aCars.push({make: “Honda”, model: “Accord”, extColor: “red”});
aCars.push({make: “Volvo”, model: “242 DL”, extColor: “red”});
aCars.push({make: “Oldsmobile”, model: “Alero”, extColor: “red”});
aCars.push({make: “Honda”, model: “Accord”, extColor: “gold”});
aCars.push({make: “Volvo”, model: “242”, extColor: “white”});
aCars.push({make: “Oldsmobile”, model: “Aurora”, extColor: “silver”});
aCars.push({make: “Honda”, model: “Prelude”, extColor: “silver”});
aCars.push({make: “Volvo”, model: “242”, extColor: “red”});
aCars.sortOn([“make”,”mode”,”extColor”]);
displayArray(aCars);
In this example the Output panel will display the following:
Honda Accord gold
Honda Accord red
Honda Prelude silver
Oldsmobile Alero blue
Oldsmobile Alero red
Oldsmobile Aurora silver
Volvo 242 DL red
Volvo 242 red
Volvo 242 white
ct
Reversing an Array
aEmployees.reverse();
bje