This Blog contains About Action script 3.0 Variables, Data types, Access specifies, Keywords, Operators, Conditional statements, Control statements, Arrays (Indexed, Associative, Multidimensional, Cloning), Functions, Symbols, Movie clips (Transformation point, Registration point, Loading images), Buttons, Events (Key board, Mouse, Timer, XML)Pre- defined function :- Random, Hit test object, Drag and drop, Sort, Loader, Tween,URL Loader, Video components, Audio Components, Sound Channel.
Wednesday, 28 November 2012
Arrays of Objects
One of the most useful structures in
Flash --especially when dealing with xml or database table data and list-type
components like ComboBox and DataGrid-- is an array of objects. Because you can
define any number of 'fields' as the properties of an object, and any number of
'records' in an array, the two make a good recordset-like combination.
The following code, for example,
creates an array of objects that is sorted alphabetically by title and used as
the data provider for a combobox component. The combobox (instancename
bookchoices) displays a list of book titles. Each entry (element of the array)
is associated with other data (the other properties of the object in that
element) which could be used to populate an order form, saved in a database, or
emailed to an interested party when selected from the dropdown by a site
visitor:
var
books:Array = [
{title:"Flash book 1",
author:"abc", pubyr:2001},
{title:"Flash book 2",
author:"def", pubyr:2000},
{title:"Flash book 3",
author:"ghi", pubyr:2001},
{title:"Flash book 4",
author:"Robert Penner", pubyr:2002}
];
books.sortOn("title");
bookchoices.labelField
= "title";
bookchoices.dataProvider
= books;
Accessing properties of an object
There are two ways to access a
property of an object. One way is with dot syntax:
var
booktitle:String = eas2book.title;
and the other way is with a
string index (treating the object as what's called an associative array):
var
booktitle:String = eas2book["title"];
The former is more compact; the
latter is useful when properties are to be accessed with a variable
representation or in a loop, eg:
var
field:String = "title";
var
booktitle:String = eas2book[field];
trace(booktitle); // Essential Actionscript 2.0
I'll repeat that because it's
important to remember:
Accessing properties of an object
There are two ways to access a property in an object. One is with dot syntax:
eas2book.title;
and the other is with a string index:
eas2book["title"];
There are two ways to access a property in an object. One is with dot syntax:
eas2book.title;
and the other is with a string index:
eas2book["title"];
Objects in action script 3.0
Instances of the Object class can be
assigned their own properties within the instance. This is the class from which
most of Flash's built-in classes are derived (subclassed from) and so is a very
useful one to become familiar with. A new variable (instance) of the Object
class is created like this:
var
eas2book:Object = new Object();
or, the abbreviated way, with curly
brackets:
var
eas2book:Object = {};
or, to declare and assign a value at
once:
var
eas2book:Object = {
title:"Essential Actionscript
2.0",
author:"Colin Moock",
pubyr:2004
};
In that example, a new instance of
the Object class named eas2book is created, with 3 properties: title, author,
and pubyr. (Each property, as noted previously, has its own data type, though
those are not explicitly declared when the object is created).
Arrays in action script 3.0
An array is a collection of pieces
of data all of the same type (typically, but the compiler doesn't check for
that) that are accessible by a numeric index. For example, the strings "cse",
"eee", and "ece" might all be stored in an array named btech_courses,
and if stored in that order, the first element would be accessible as btech_courses[0]
(the indexing always starts with 0) and the last would be btech_courses[2].
var
btech_courses:Array = ["cse", "eee", "ece"];
trace('the
first element is ' + btech_courses[0] + ' and the last element is ' + btech_courses[2]);
Pasting that code into a Flash movie
and ruuning it produces this in the Output panel: the first element is cse and the last element is ece. Like the String class, the Array
class has several methods that are indispensable for manipulating arrays.
Here's an example sequence of array statements, with the output from each:
|
Operation
|
Result
of trace statement
|
|
Create a 3-element array of information sources:
var sources:Array;
sources = new
Array("google","about","altavista");
trace("sources has " + sources.length + "
elements");
or, the abbreviated way (note the
square brackets):
var sources:Array = ["google",
"about", "altavista"];
|
sources has 3 elements
|
|
Use push method to add
another element at the end of the array
sources.push("metacrawler");
trace(sources);
|
google,about,altavista,metacrawler
(Note that running a trace on an array causes all elements to be dumped out as strings, one at a time, separated by commas) |
|
Use reverse method to
reverse order of array
sources.reverse();
trace("first element is now " + sources[0]);
|
first element is now metacrawler
|
|
pop the array to remove the last element
sources.pop();
trace(sources);
|
metacrawler,altavista,about
|
|
Use splice to insert
elements in the middle of the array
sources.splice(2, 0, "atomz",
"lycos");
trace(sources);
|
metacrawler,altavista,atomz,lycos,about
The 2 specifies that splicing will begin at the third element of the array; the 0 specifies that no elements are to be deleted (since splicing can be used either for adding elements to or deleting elements from an array); the last two parameters are array elements to insert into the array. |
|
Use splice to delete an
element in the array
sources.splice(3, 1);
trace(sources);
|
metacrawler,altavista,atomz,about
The 3 specifies that splicing will begin at the third element of the array; the 1 specifies that one element is to be deleted. |
|
Use unshift to add elements
to the beginning of the array
sources.unshift("dogpile");
trace(sources);
|
dogpile,metacrawler,altavista,atomz,about
|
|
sort the array alphabetically
sources.sort();
trace(sources);
|
about,altavista,atomz,dogpile,metacrawler
|
Subscribe to:
Posts (Atom)

