McGiver
09-10-2003, 12:25 PM
come on everybody! it's flashing time! :D
Flash MX04 is availible, and what we need are tutorials! so everybody who has figured out some of the new mx04 language elements:
please write a tutorial about it!
what can i use this or that for, and especially what are classes and how to use them!
Colin Campbell
09-10-2003, 03:49 PM
Classes Tutuorial From Flash MX 2004:
Creating classes with ActionScript 2.0 overview
ActionScript 2.0 is a restructuring of the ActionScript language that provides several powerful new programming features found in other programming languages, such as Java. ActionScript 2.0 encourages program structures that are reusable, scalable, robust, and maintainable. It also decreases development time by providing users with thorough coding assistance and debugging information. ActionScript 2.0 conforms to existing standards and is based on the ECMAScript 4 proposal (www.mozilla.org/js/language/es4/). ActionScript 2.0 is available in Macromedia Flash MX 2004 and Macromedia Flash MX Professional 2004.
The features of ActionScript 2.0 are described below.
Familiar object-oriented programming (OOP) model The primary feature of ActionScript 2.0 is a familiar model for creating object-oriented programs. ActionScript 2.0 introduces several new object-oriented concepts and keywords such as class, interface, and packages that will be familiar to you if you've ever programmed with Java.
The OOP model provided by ActionScript 2.0 is a "syntactic formalization" of the prototype chaining method used in previous versions of Macromedia Flash to create objects and establish inheritance.
Strict data typing ActionScript 2.0 also lets you explicitly specify data types for variables, function parameters, and function return types. For example, the following code declares a variable named userName of type String (a built-in ActionScript data type, or class).
var userName:String = "";
Compiler warnings and errors The above two features enable the authoring tool and compiler to provide compiler warnings and error messages that help you find bugs in your applications faster than was previously possible in Flash.
Caution: If you plan to use ActionScript 2.0 syntax, ensure that the Publish settings for the FLA file specify ActionScript 2.0. This is the default for files created in Flash MX 2004. However, if you open an older FLA file that uses ActionScript 1 and begin rewriting it in ActionScript 2.0, change the Publish Settings of the FLA file to ActionScript 2.0. If you don't do so, your FLA file will not compile correctly, but no errors will be generated.
Object-oriented programming principles
This section provides a brief introduction to principles involved in developing object-oriented programs. These principles are described in more depth in the rest of this chapter, along with details on how they are implemented in Macromedia Flash MX 2004 and Macromedia Flash MX Professional 2004.
Objects
Think of a real-world object—for example, a cat. A cat could be said to have properties (or states) such as name, age, and color; a cat also has behaviors such as sleeping, eating, and purring. In the world of object-oriented programming, objects also have properties and behaviors. Using object-oriented techniques, you can model a real-world object (like a cat) or a more abstract object (like a chemical process).
Classes and class members
Continuing with the real-world analogy, consider that there are cats of different colors, ages, and names, with different ways of eating and purring. But all cats belong to a certain class of object, an object of type "cat." Each individual (real- world) cat is an instance of the cat class type.
Likewise, in object-oriented programming, a class defines a blueprint for a type of object. The characteristics and behaviors that belong to a class are referred to as members of that class. The characteristics (in the cat example, name, age, and color) are called properties of the class, which are represented as variables; the behaviors (eating, sleeping) are called methods of the class, and are represented as functions.
For example, you could create a Person class, and then create an individual person that would be an instance of that class, also called a Person object. The Person object would contain all the properties and methods of the Person class.
In ActionScript, you define a class with the class statement (see Creating and using classes). ActionScript includes a number of built-in classes, such as the MovieClip, TextField, and String classes. For more information, see Using the Built-In Classes.
Inheritance
One of the primary benefits of object-oriented programming is that you can create subclasses of a class; the subclass then inherits all the properties and methods of the superclass. The subclass typically defines additional methods and properties, or extends the superclass. Subclasses can also override (provide their own definitions for) methods inherited from a superclass.
For example, you might create a Mammal class that defines certain properties and behaviors common to all mammals. You could then create a Cat class that extends the Mammal class. In this way, inheritance can promote code reuse: instead of recreating all the code common to both classes, you can simply extend an existing class. Another subclass, in turn, could extend the Cat class, and so on. In a complex application, determining how to structure the hierarchy of your classes is a large part of the design process.
In ActionScript, you use the extends keyword to establish inheritance between a class and its superclass. For more information, see Creating subclasses.
Interfaces
Interfaces in object-oriented programming can be described as classes whose methods are not implemented (defined). Another class can implement the methods declared by the interface.
An interface can also be thought of as a "programming contract" that can be used to enforce relationships between otherwise unrelated classes. For example, suppose you are working with a team of programmers, each of whom is working on a different part (class) of the same application. While designing the application, you agree on a set of methods that the different classes will use to communicate. So you create an interface that declares these methods, their parameters, and their return types. Any class that implements this interface must provide definitions for those methods; otherwise, a compiler error will result.
You can also use interfaces to provide a limited form of "multiple inheritance," which is not allowed in ActionScript 2.0. In multiple inheritance, a class extends more than one class. For example, in C++ the Cat class could extend the Mammal class, as well as a Playful class, which has methods ChaseTail and EatCatNip. ActionScript 2.0, like Java, does not allow a class to extend multiple classes directly. However, you could create a Playful interface that declares the ChaseTail and EatCatNip methods. A Cat class, or any other class, could then implement this interface and provide definitions for those methods.
Using classes example
For those who are new to object-oriented programming, this section provides an overview of the workflow involved in creating and using classes in Flash. At a minimum, this workflow involves the following steps:
Defining a class in an external ActionScript class file.
Saving the class file to a designated classpath directory (a location where Flash looks for classes).
Creating an instance of the class in another script, either in a Flash (FLA) document or an external script file, or creating a subclass based the original class.
Also discussed in this section is a new feature in ActionScript 2.0 called strict data typing, which lets you specify the data type for a variable, function parameter, or function return type.
Although this section discusses only classes, the general workflow is the same for using interfaces. For more information, see Creating and using interfaces.
Creating a class file
To create a class, you must first create an external ActionScript (AS) file. Classes (and interfaces) can only be defined in external script files. For example, you can't define a class in a script attached to a frame or button in a Flash document (FLA). To create an external AS file, use the ActionScript editor included with Flash or your preferred code or text editor.
Note: ActionScript code in external files is compiled into a SWF file when you publish, export, test, or debug a FLA file. Therefore, if you make any changes to an external file, you must save the file and recompile any FLA files that use it.
In the steps below you'll create a class called Person that contains two properties (age and name) and a single method (showInfo()) that displays the values of those properties in the Output panel.
To create the class file:
Create a new directory on your hard disk and name it PersonFiles. This directory will contain all the files for this project.
Do one of the following:
Create a new file in your preferred text or code editor.
(Flash Professional only) Select File > New to open the New Document dialog box, select ActionScript File from the list of file types, and click OK. The Script window opens with a blank file.
Save the file as Person.as in the PersonFiles directory.
In the Script window, enter the following code:
class Person {
}
This is called the class declaration. In its most basic form, a class declaration consists of the class keyword, followed by the class name (Person, in this case), and then left and right curly braces ({}). Everything between the braces is called the class body and is where the class's properties and methods are defined.
Note: The name of the class (Person) matches the name of the AS file that contains it (Person.as). This is very important; if these two names don't match, the class won't compile.
To create the properties for the Person class, use the var keyword to define two variables named age and name, as shown below.
class Person {
var age:Number;
var name:String;
}
Tip: By convention, class properties are defined at the top of the class body, which makes the code easier to understand, but this isn't required.
Notice the colon syntax (var age:Number and var name:String) used in the variable declarations. This is an example of strict data typing. When you type a variable in this way (var variableName:variableType), the ActionScript 2.0 compiler ensures that any values assigned to that variable match the specified type. Although this syntax is not required, it is good practice and can make debugging your scripts easier. (For more information, see Strict data typing.)
Next you'll create the showInfo() method, which returns a preformatted string containing the values of the age and name properties. Add the showInfo() function definition to the class body, as shown below.
class Person {
var age:Number;
var name:String;
// Method to return property values
function showInfo():String {
return("Hello, my name is " + name + " and I'm " + age + " years old.");
}
}
Notice the use of data typing (optional but recommended) in the function definition.
function showInfo():String {...}
In this case, what's being typed is the showInfo() function's return value (a string).
The last bit of code you'll add in this section is a special function called a constructor function. In object-oriented programming, the constructor function initializes each new instance of a class.
The constructor function always has the same name as the class. To create the class's constructor function, add the following code:
class Person {
var age:Number;
var name:String;
// Method to return property values
function showInfo():String {
return("Hello, my name is " + name + " and I'm " + age + " years old.");
}
// Constructor function
function Person (myName:String, myAge:Number) {
name = myName;
age = myAge;
}
}
The Person() constructor function takes two parameters, myName and myAge, and assigns those parameters to the name and age properties. The two function parameters are strictly typed as String and Number, respectively. For more information about constructor functions, see Constructor functions.
Note: If you don't create a constructor function, an empty one is created automatically during compilation.
Save the file as Person.as in the PersonFiles directory that you created in step 1.
If you're using Flash MX 2004 (not Flash Professional), see Creating an instance of the Person class.
(Flash Professional only) Check the syntax of the class file by selecting Tools > Check 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 in step 7, above. If you can't fix the code errors, copy the completed code in step 7.
_______________________________
Thats not all of it, but I hope that suffices until someone writes a tutorial on it.
WARNING: Some of tutorial may not be relavent!
McGiver
09-10-2003, 05:52 PM
wow! that's kind of impressing for the short time!
I haven't got really time to get into the new flash at the moment, but i promise I'll give it a try over the weekend.
Colin Campbell
09-10-2003, 05:53 PM
again, its one of the tutorials included with Flash MX 2004 :p I didn't write it
Mortimer Jazz
09-30-2003, 09:23 AM
Tutorials now in the ..erm.. tutorials section (funnily enough :D )
Jesse
09-30-2003, 11:34 AM
Colin, if you wrote that tutorial (above) and are willing to let us host it, please email me and I'll post it.
Colin Campbell
09-30-2003, 06:55 PM
Originally posted by colin14
again, its one of the tutorials included with Flash MX 2004 :p I didn't write it
sorry Jesse... if I had wrote it though, I would have submitted it by now. :)
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.