Actionscript Course

Arrays are variables that store multiple values in a single variable name. So, an Array is a variable with an ordered list of values.

Creating Array

In ActionScript 3 there are two ways to create an Array: with square brackets [] (also known as JSON syntax), and with the Array object.

1) Syntax for Array created with square brackets:
var array_name:Array = [val1, val2, ...];

2) Syntax with Array object:
var array_name = new Array(val1, val2, ...);

- "array_name" - is the name of the Array, and will be ussed to access its elements (can be any name used for variables).
- "val1, val2, ..." - are the values (elements) stored in the Array. An element can be any type of value: String, Number, Object or eaven another array.
For example:
var m_ar:Array = ['some string', a_variable, 7.8];
OR:
var m_ar:Array = new Array('some string', a_variable, 7.8);

To create an empty Array, you can use any of these two expresions:
                  var m_ar:Array = [];       OR       var m_ar:Array = new Array();

Using Array elements

Once an Array is created, you can access and use its items.
Arrays are structured as a series of index-value pairs, where one pair is an element of that array. For each item in the list, there is an index (an integer) associated with it.
So, the array elements are stored in numerical order, starting from 0. The first element has index 0, the second index 1, and so on.
To access an element of an Array, write the array name followed by the index within square brackets: array_name[index].
  - Example:
var m_ar:Array = ['web site', 'coursesweb.net', 78];

// get the second item (with index 1)
trace(m_ar[1]);         // Output:  coursesweb.net
To access the first value, use index 0 (e.g., trace(m_ar[0]); will output "web site").

An array item accessed in this way (array_name[index]) can be used as any variable, in operations with other values and variables.
Also, you can use a variable for the index value.
Let's see an example:
var m_ar:Array = ['web site', 'coursesweb.net', 78];

// define a variable to be used for index
var i:uint = 2;

// create a variable which use an array item for its value
var test = 4 + m_ar[i];

trace(test);        // Output: 82
- Because the value of "i" variable is 2, the expression m_ar[i] will access the third element in "m_ar" and return its value (78).

Modify and Add values in an Array

To modify a value in an array, assign a new value to the element with a specified index number.
  Syntax:
array_name[index] = new_value;
If the array_name[index] element isn't defined in that array, will be added automatically with the "new_value". This method can be used when you want to add new elements in an array.
  See the next example and the explanations in it:
var m_ar:Array = ['web site', 'coursesweb.net', 78];

// changes the value of the first element
m_ar[0] = 'tutorials';

// use "trace(m_ar)" to check the Array values
trace(m_ar);        // tutorials,coursesweb.net,78

// adds a new element in "m_ar" (fourth, with index 3)
m_ar[3] = 'ActionScript';

// use "trace(m_ar)" to check the Array values
trace(m_ar);        // tutorials,coursesweb.net,78,ActionScript
- "trace(array_name)" displays in the Output panel the values stored in array_name, separated by comma.
The example above will display in Output:
tutorials,coursesweb.net,78
tutorials,coursesweb.net,78,ActionScript

• To delete the value of an Array element, just assign it an empty value:   array_name = "";.
The item will remain in the array, but with no value.

To delete completely an intem from an Array, use the slice() method.
  Syntax:
array_name.slice(index, 1)
- "index" - is the index of the item you want to delete. The value of 1 specifies that one element will be deleted.
  Example:
var m_ar:Array = ['web site', 'coursesweb.net', 78];

// delete the value of the third item in "m_ar"
m_ar[2] = '';

// delete the second item in "m_ar"
m_ar.splice(1, 1);

// use "trace(m_ar)" to check the Array values
trace(m_ar);        // web site,

The number of items in Array

When you work with Arrays, often it is necessary to know the number of items in that array.
To get the number of elements in an array, use the length property.
array_name.length
If in an Array, for example with three elements (with index 0, 1, and 2), you add a new elemant, but with a higher index that the next item should have, for example a fourth element, but with index 5, that Array will have 6 elements. ActionScript will automatically add the missing elements with an empty value, starting from the next index in array till the index of the new item.
  See the following example:
var m_ar:Array = ['web site', 'coursesweb.net', 78];

// define a variable to store the number of items in "m_ar"
var nrel:int = m_ar.length;

// use "trace(nrel)" to check the number of items
trace(nrel);                 // 3

// add a new element in array (fourth, but with index 5)
m_ar[5] = 'marplo.net';

// updates number of items stored in "nrel" and check again with trace()
nrel = m_ar.length;
trace(nrel);                  // 6

// check to see all values in "m_ar"
trace(m_ar);        // web site,coursesweb.net,78,,,marplo.net
As you can notice, after the fourth value was added, with index 5, the "m_ar" array has 6 elements. The items with index 3 and 4 was automaticaly added, with empty value.
The example above will display in Output:
3
6
web site,coursesweb.net,78,,,marplo.net

The length property can also be used to set a specific number of elements in an array, adding empty values if you specify a higer index, or deleting items (starting from the last) if you set a length lower then the number of items in the array.
  See the following example:
var m_ar:Array = ['web site', 78, 'marplo.net'];

// set a number of 8 items in "m_ar"
m_ar.length = 8;

// define a variable to store the number of items in "m_ar"
var nrel:int = m_ar.length;

// use "trace(nrel)" to check the number of items
trace(nrel);                 // 8

// set a number of 2 items in "m_ar"
m_ar.length = 2;

// updates number of items stored in "nrel" and check again with trace()
nrel = m_ar.length;
trace(nrel);                  // 2

// check to see all values in "m_ar"
trace(m_ar);        // web site,78

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
Arrays in ActionScript 3

Last accessed pages

  1. jsSHA - SHA Hashes and HMAC in JavaScript (3427)
  2. Download AJAX resources (219)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (137562)
  4. Recursive function to create Multi-Level Menu in PHP (11769)
  5. Force Download files with PHP (5056)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (251)
  2. Read Excel file data in PHP - PhpExcelReader (91)
  3. PHP Unzipper - Extract Zip, Rar Archives (76)
  4. The Four Agreements (75)
  5. The Mastery of Love (66)