Macromedia Flash 8 Bible Free Open Book

Macromedia Flash 8 Bible

Previous Page
Next Page

Managing Related Data: The Array Class

Have you ever had a bunch of variables that have a lot in common? For example, do you have variables such as name_1, name_2, name_3, name_4, and so on? These variables look like lists of common information, such as:

var name_1:String = "Peng";
var name_2:String = "Django";
var name_3:String = "Jenn";
var name_4:String = "Frank";

In programming languages, an array is a list of values that can be addressed by their position in the list. An array is created by using the Array constructor:

var visitors:Array = new Array();

The preceding code object simply creates the array container for data. You create an array with information already specified, such as:

var visitors:Array = new Array("Peng"," Django"," Jenn"," Frank");

or

var visitors:Array = ["Peng", "Django", "Jenn", "Frank"];

To access an item in visitors, you would use the array access operators with an array index number. To access the first position's data, you would use the following code:

var message:String = "Hello "+ visitors[0] + ", and welcome.";

Here, visitors[0] will return the value "Peng". If you traced the message variable, it would read:

Hello Peng, and welcome.

In most programming languages, the first index value (the starting position) is 0, not 1. In the following table, you'll see how the index number increases with the sample visitors array.

Index Position

0

1

2

3

Index Value

Peng

Django

Jenn

Frank

You can set and get the values within an array using the array access operators. You can replace existing array values by setting the index position to a new value, and you can add values to the array by increasing the index number, as in:

var visitors:Array = new Array("Peng"," Django"," Jenn"," Frank");
visitors[3] = "Nicole";
visitors[4] = "Candice";

In the example, "Nicole" replaces "Frank", and "Candice" is added to the end of the array. You can also add elements to the array using the push() method of the Array class, as in:

var visitors:Array = new Array("Peng"," Django"," Jenn"," Frank");
var nLength:Number = visitors.push("Nicole"," Candice");

This code will add "Nicole" and "Candice" after "Frank", and set the variable nLength equal to the length of the visitors array. length is an Array property that returns the number of elements in the array. In the preceding example, nLength is equal to 6 because there are now six names in the array.


Previous Page
Next Page
Index: [A][B][C][D][E][F][G][H][I][J][K][L][M][N][O][P][Q][R][S][T][U][V][W][X][Y][Z]


     Main Menu
Table of Contents
Back Cover
Macromedia Flash 8 Bible
Foreword
Preface
Part I: An Introduction to Flash Web Production
Part II: Mastering the Flash Environment
Part III: Creating Animation and Effects
Part IV: Integrating Media Files with Flash
Part V: Adding Basic Interactivity to Flash Movies
Part VI: Distributing Flash Movies
Part VII: Approaching ActionScript
Chapter 24: Knowing the Nuts and Bolts of Code
Chapter 25: Controlling Movie Clips
Chapter 26: Using Functions and Arrays
What are Data Types?
Overview of Functions as Procedures
Managing Related Data: The Array Class
Creating a Dynamic Reusable Flash Menu
Functions as Methods of Objects
Functions as Constructors for Objects
Summary
Chapter 27: Interacting with Movie Clips
Part VIII: Applying ActionScript
Part IX: Integrating Components and Data-Binding
Part X: Expanding Flash
Part XI: Appendixes
Index
List of Figures
List of Tables
List of Listings
List of Sidebars


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele