About working with built-in classes
297
For example, the following code creates a new Sound object:
var song_sound:Sound = new Sound();
In some cases, you don’t need to create an instance of a class to use its properties and methods.
For more information, see
“About class (static) members” on page 298
.
Accessing built-in object properties
Use the dot (
.
) operator to access the value of a property in an object. Put the name of the
object on the left side of the dot, and put the name of the property on the right side. For
example, in the following statement,
my_obj
is the object and
firstName
is the property:
my_obj.firstName
The following code creates a new Array object and then shows its
length
property:
var my_array:Array = new Array("apples", "oranges", "bananas");
trace(my_array.length); // 3
You can also use the array access operator (
[]
) to access the properties of an object, such as
using the array access operator for debugging purposes. The following example loops over an
object to display each of its properties.
To loop over the contents of an object:
1.
Create a new Flash document and save it as
forin.fla
.
2.
Add the following ActionScript to Frame 1 of the main Timeline:
var results:Object = {firstName:"Tommy", lastName:"G", age:7, avg:0.336,
b:"R", t:"L"};
for (var i:String in results) {
trace("the value of [" + i + "] is: " + results[i]);
}
The previous code defines a new Object named results and defines values for
firstName
,
lastName
,
age
,
avg
,
b
, and
t
. A
for..in
loop traces each property in the results object
and traces their value to the Output panel.
3.
Select Control > Test movie to test the Flash document.
For more information on operators, including dot and array access operators, see
“About
operators” on page 176
. For more information on methods and properties, see
Chapter 6,
“Functions and Methods,” on page 201
. For examples of working with properties of the built-
in MovieClip class, see
Chapter 11, “Working with Movie Clips,” on page 351
For examples
of working with the properties of the TextField, String, TextRenderer, and TextFormat classes,
see
Chapter 12, “Working with Text and Strings,” on page 381
.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...