MACROMEDIA FLASH MX 2004-USING ACTIONSCRIPT IN FLASH Use Manual page 253

Using actionscript in flash
Hide thumbs Also See for FLASH MX 2004-USING ACTIONSCRIPT IN FLASH:
Table of Contents

Advertisement

// Constructor function
function Person (myName:String, myAge:Number) {
this.name = myName;
this.age = myAge;
}
// Method to return property values
function getInfo():String {
return("Hello, my name is " + this.name + " and I'm " + this.age + "
years old.");
}
}
This code is the completed code for this class. The return value of the
strictly typed (optional, but recommended) as a string.
Save the file.
9.
If you're using Flash MX 2004 (not Flash Professional), proceed to
Person class" on page
(Optional, Flash Professional only) Check the syntax of the class file by selecting Tools > Check
10.
Syntax, or pressing Control+T (Windows) or Command+T (Macintosh).
If any errors are reported in the Output panel, compare the code in your script to the final
code. If you can't fix the code errors, copy the completed code.
You've successfully created a class file. To continued with this example, see
of the Person class" on page
Creating an instance of the Person class
The next step is to create an instance of the Person class in another script, such as a frame script in
a Flash (FLA) document or another AS script, and assign it to a variable. To create an instance of
a custom class, you use the
built-in ActionScript class (such as the Date or Error class). You refer to the class using its fully
qualified class name or import the class; see
Continuing the example you started in
in the same directory as the class file you created, you can refer to the class file using the fully
qualified class name, which is
Person class and assigns it to the variable
var newPerson:Person = new Person("Nate", 32);
This code invokes the Person class's constructor function, passing as parameters the values
and 32.
The
newPerson
compiler to ensure that you don't try to access properties or methods that aren't defined in the
class. See
"Strict data typing" on page
using the
dynamic
253.
253.
operator, the same as you would when creating an instance of a
new
. For example, the following code creates an instance of the
Person
variable is typed as a Person object. Typing your objects in this way enables the
41. (The exception is if you declare the class to be dynamic
keyword. See
"Creating dynamic classes" on page
"Importing classes" on page
"Creating a class file" on page
:
newPerson
getInfo()
"Creating an instance of the
"Creating an instance
271.
251, if you create a FLA file
259.)
Using classes: a simple example
function is
"Nate"
253

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH MX 2004-USING ACTIONSCRIPT IN FLASH and is the answer not in the manual?

Questions and answers

This manual is also suitable for:

Flash mx 2004 - actionscript

Table of Contents