Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0  
 
by Dave Yang, quantumwave.com - swfoo.com
Download Source Files 
 
ActionScript 2.0
 

01. Introduction
02. A little bit of OOP in ActionScript 1.0 
03. What's new in ActionScript 2.0?

04. New keywords and features for OOP
05. Scope and "this"
06. Private or protected?
07. Dynamic vs Static classes

08 Inheritance.
09. Overriding, Overwriting or Overloading?

10. Importing external class files
11. Interface
12. What is missing in ActionScript 2.0?
13. Conclusion

- discuss this tutorial -

03. What's new in ActionScript 2.0?

ActionScript 2.0 is not a new language, it is an enhancement to the previous version. Learning it is actually quite easy if you have already been programming in ActionScript 1.0 (or if you have other OO language background). Let take a look at what ActionScript 2.0 offers:

  • Strict data typing and improved compiler warnings

  • Code hinting based on data type

  • New keywords and features for OOP:

    • class
    • interface
    • extends
    • implements
    • public
    • private
    • static
    • dynamic
    • intrinsic (intended for Macromedia internal use only)
    • import
    • class path (package)
    • get
    • set

Let's take a look at each one of these new features.

Strict data typing

In strongly-typed languages, expressions are associated with data types. The three major benefits of strict typing are:

It helps the compiler to detect potential problems and catch data type mismatch errors.
It makes code easier to read and understand with clearly specified data types .
It allows early-binding of data into appropriate object types for runtime optimization.
Because Flash MX 2004 only supports compile-time strict type checking, defined data types are not exported to SWF files and there is no runtime type checking or early-binding optimization. Also, ActionScript 2.0 does not support machine native data types such as those found in other languages (including JScript.NET): int, long, float...etc.

In ActionScript 1.0, declaring a variable 'count' looks like this:

var count;

In ActionScript 2.0, with strict typing, this statement can be written as:

var count:Number;

Note: The syntax <variable> : <data type> may seem strange to some developers (languages such as Java, C/C++ and C# declare the type before the variable). This was a decision that the ECMA-262 committee made, and Macromedia is simply following the specifications.

ActionScript 2.0 supports strict typing for variables, function arguments and return values. For example, the following function accepts one argument of type String, and returns a Boolean value.

function func(arg:String):Boolean {};

When a function does not accept any argument or return any value, use Void as the data type:

function func(Void):Void {};

The way the argument is written (as Void) may seem strange, but it is used throughout component code. It simply signifies that there are no expected argument. However, if you try to pass arguments to this function, the compiler does not generate any error warnings, and the function will receive the arguments.

Although strict typing is available in ActionScript 2.0, Flash does not enforce the strict typing syntax. However, if the compiler checks and finds a mismatch with one of the classes that is being used, a warning will be displayed. It is a good habit to start using strict typing because of the benefits mentioned above. If you do use strict typing, you must specify "ActionScript 2.0" in the movie publish settings. However, you don't have to publish to Flash Player 7 as AS2 can be compiled for Flash Player 6 (even better, choose to export for player version 6.0.65.0 (6 r65) or higher for better optimization).

With strict typing, the new compiler now offers many new warnings and prevents movies with mismatched data types from compiling. This can be a big time saver when it comes to debugging code.

Code Hinting

In Flash MX, code hinting is usually based on variable name extensions such as _pb for Push Button, or _cb for ComboBox (another method is to declare the code hinting pattern in comment-styles notations such as // MovieClip myClip;). In Flash MX 2004, you'll find that most of these extensions no longer work by default. The new version 2 components use a different approach to code hinting.

When a variable type is specified (and if the code hint information for the class is available), methods and properties of the class are displayed at the end of the dot; otherwise, no code hinting is displayed. By specifying the data type, typing the following brings up the code hint dialog box for ComboBox:

var combo:mx.controls.ComboBox;
combo.



You can check out the default name extensions by opening the following file in a text or XML editor:

<Flash installation folder>\<language>\First Run\ActionsPanel\AsCodeHints.xml

For English installations, <language> is "en".

Tip: Adding Flash MX's code hints to Flash MX 2004

If you are using Flash MX 2004 to edit Flash MX movies with version 1 components, or when you need to publish for playback in all versions of the Flash Player 6, you can make some changes to use Flash MX's default code hints:

Open and copy the <codehints> section from the end of Flash MX's UIComponents.xml (located at <Flash MX installation folder>\<language>\First Run\ActionsPanel\CustomActions\UIComponents.xml), as shown below.

         <codehints>
             <typeinfo pattern="*_ch" object="FCheckBox"/>
             <typeinfo pattern="*_pb" object="FPushButton"/>
             <typeinfo pattern="*_rb" object="FRadioButton"/>
             <typeinfo pattern="*_lb" object="FListBox"/>
             <typeinfo pattern="*_sb" object="FScrollBar"/>
             <typeinfo pattern="*_cb" object="FComboBox"/>
             <typeinfo pattern="*_sp" object="FScrollPane"/>
             <typeinfo pattern="globalStyleFormat" object="FStyleFormat"/>
         </codehints>
         

Paste it just before the ending tag of Flash MX 2004's UIComponents.xml.
Paste the same block in Flash MX 2004's AsCodeHints.xml (but remove the <codehints></codehints> tags)
This lets you use Flash MX version 1 component code hints in Flash MX 2004.

Another method to bind variable names to (most) built-in data types is to use the comment-style notation (that Flash MX also supports):

// DataType variable;

For example, if you want to declare the variable myClip as a MovieClip, you can place this code before you use the variable:

// MovieClip myClip;

From now on, typing myClip. brings up code hinting for MovieClip. However, this does not work with non-built-in classes such as mx.controls.ComboBox; for these, use the var notation as discussed above instead.


- discuss this tutorial -
 
©2003 Ultrashock.com - All rights reserved