The User class's constructor statement takes two parameters:
, which are copied into the class's private instance variables
p_password
. The remainder of the code in the class defines the getter and setter
__password
properties for the private instance variables. If you want to create a read-only property,
then you would define a getter function, but not a setter function. For example, if you
want to make sure a user name cannot be changed after it has been defined, you would
delete the
username
5.
Select File > New and then select Flash Document.
6.
Select File > Save As and name the file user_test.fla. Save the file in the same directory as
User.as.
7.
Type the following ActionScript into Frame 1 of the Timeline:
import User;
var user1:User = new User("un1", "pw1");
trace("Before:");
trace("\t username = " + user1.username); // un1
trace("\t password = " + user1.password); // pw1
user1.username = "1nu";
user1.password = "1wp";
trace("After:");
trace("\t username = " + user1.username); // 1nu
trace("\t password = " + user1.password); // 1wp
Because the User class you created previously is very basic, the ActionScript in the Flash
document is also very straightforward. The first line of code imports the custom User class
into your Flash document. Importing the User class lets you use the class as a custom data
type.
A single instance of the User class is defined and assigned to a variable named
assign the
user1
. The following two
pw1
and
user1.password
The next two lines use the User class's setter functions to set new values for the
and
variables. Finally, you trace the values for
password
Output panel. The
setter functions.
8.
Save the FLA file, and then select Control > Test Movie to test the files.
You see the results of the
you use these files in an application.
setter function in the User class file.
User object a value and define a
statements display the current value of
trace
using the User class's getter functions, which both return strings.
statements display the modified values that you set using the
trace
statements in the Output panel. In the next examples,
trace
p_username
of
and a
username
un1
user1.username
and
username
Writing custom class files
and
and
__username
. You
user1
of
password
username
to the
password
237
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?