42
Chapter 2: ActionScript Basics
Local variables
To declare local variables, use the
var
statement inside the body of a function. A local variable is
scoped to the block and expires at the end of the block. A local variable not declared within a
block expires at the end of its script.
For example, the variables
i
and
j
are often used as loop counters. In the following example,
i
is
used as a local variable; it exists only inside the function
makeDays()
:
function makeDays() {
var i;
for( i = 0; i < monthArray[month]; i++ ) {
_root.Days.attachMovie( "DayDisplay", i, i + 2000 );
_root.Days[i].num = i + 1;
_root.Days[i]._x = column * _root.Days[i]._width;
_root.Days[i]._y = row * _root.Days[i]._height;
column = 1;
if (column == 7 ) {
column = 0;
row = row + 1;
}
}
}
Local variables can also help prevent name conflicts, which can cause errors in your application.
For example, if you use
name
as a local variable, you could use it to store a user name in one
context and a movie clip instance name in another; because these variables would run in separate
scopes, there would be no conflict.
It’s good practice to use local variables in the body of a function so that the function can act as an
independent piece of code. A local variable is only changeable within its own block of code. If an
expression in a function uses a global variable, something outside the function can change its
value, which would change the function.
You can assign a data type to a local variable when you define it, which helps prevent you from
assigning the wrong type of data to an existing variable. For more information, see
“Strict data
typing” on page 38
.
Timeline variables
Timeline variables are available to any script on that Timeline. To declare Timeline variables,
initialize them on any frame in the Timeline. Be sure to initialize the variable before trying to
access it in a script. For example, if you put the code
var x = 10;
on Frame 20, a script attached
to any frame before Frame 20 cannot access that variable.
Содержание FLASH MX 2004 - ACTIONSCRIPT
Страница 1: ...ActionScript Reference Guide...
Страница 8: ...8 Contents...
Страница 12: ......
Страница 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Страница 54: ...54 Chapter 2 ActionScript Basics...
Страница 80: ...80 Chapter 3 Writing and Debugging Scripts...
Страница 82: ......
Страница 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Страница 112: ......
Страница 120: ...120 Chapter 6 Using the Built In Classes...
Страница 176: ......
Страница 192: ...192 Chapter 10 Working with External Data...
Страница 202: ...202 Chapter 11 Working with External Media...
Страница 204: ......
Страница 782: ...782 Chapter 12 ActionScript Dictionary...
Страница 793: ...Other keys 793 221 222 Key Key code...
Страница 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Страница 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Страница 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Страница 816: ...816 Index...