Basic Syntax

 <  Day Day Up  >  

ActionScript 2.0 is a much more "strict" language than previous versions of ActionScript. As a best practice, you need to strictly type all your variables, although Flash will not fall over if you do not do this. However, Flash will provide better error handling and code hinting if you type all variables .

There are some situations where Flash will generate errors. For example, if you do not keep in mind that ActionScript is now a case-sensitive language or if you try to define classes on a timeline instead of in a separate file, Flash will have problems. We discuss each of these issues in turn .

Case Sensitivity

There are some changes Flash developers will have to get used to in ActionScript 2.0. First off, ActionScript 2.0 is a case-sensitive language. The following code displays as undefined because of the mixed case of the variable myProduct . In contrast to ActionScript 1.0, a variable must be referenced with the appropriate case.

 var myProduct = "Call Center Server"; trace (myproduct); 

Case sensitivity can be a big issue when using ActionScript 2.0 to develop for Flash Player 6 because case-sensitive variables can conflict. For example, Flash Player 7 would interpret the following code as two different variables, but Flash Player 6 would see these two as the same variable:

 var myProduct = "Call Center Server"; var myproduct = "Call Center Client"; 

At its core , ActionScript is still interpreted by the Flash Player and this can cause conflicts at runtime (not compile time) when all the error checking occurs. In this situation, the Flash developer is responsible for debugging his or her own code; of course, this is not an issue if you use ActionScript 1.0, which is not a case-sensitive language (in most cases).

N OTE

The preceding ActionScript code is a definite bad practice; never use the same variable name and different case to create two different variables.


Typing Variables

Typing variables is purely a compile-time feature and it enables much more sophisticated error handling than any previous version of Flash. It is also how this version of Flash handles code hinting in the ActionScript editor.

Every class in ActionScript 2.0 has its own datatype, and each time that class is declared and instantiated , it should be preceded by the var keyword and its datatype. For example, if you created an object from the Object class, also known as instantiation, the following code would suffice:

 myObject = new Object(); 

However, for code hinting and error handling, it would be a better practice to use the following code:

 var myObject :Object = new Object(); 

Code Hinting

For code hinting to work with visual objects (such as the user interface [UI] components ), you need to formally declare those objects in your ActionScript. Developers should always specify explicitly from what class a visual object is derived. At first, this might seem like a chore, but it is worth it. The following code shows variable typing for different types of objects, including strings, numbers , objects, and a visual object, ComboBox .

 var productName:String = "Call Center Server" var productNo:Number =2; var myObject:Object = new Object(); var myCombo:mx.controls.ComboBox; 

The following ActionScript code results in a syntax error because variable typing is not supported in this manner:

 var myProduct:Object = new Object(); myProduct.prodName:String = "Call Center Server"; 

Flash does not support this syntax because it is usually much more effective to define the variable within a class, rather than as attached to an object. Only variables defined directly on a timeline or in a class can be typed. The following code would also result in an error:

 _parent.myVariable :String = "Call Center Server"; 

Strict variable typing is in stark contrast to ActionScript 1.0 where loosely typed variables were the rule. It is something that all Flash developers should get used to because it greatly enhances the debugging capabilities in ActionScript. All variable types are checked at compile time, and if there is a type mismatch, for example, a number is assigned to a string, Flash generates an error message telling the developer what is wrong. Mismatches can occur during assignment operations, function calls, and class member dereferencing using the . operator. All classes used in ActionScript are valid as datatypes, including any custom classes that the developer has created.

N OTE

An error does not result if a datatype is not specified, but it is a best practice to always specify a datatype when defining a variable or creating an object.


Datatypes

Note these recognized datatypes. Every one, except for Void , is the name of a class in ActionScript and should be handled with care:

  • Array

  • Boolean

  • Button

  • Color

  • CustomActions

  • Date

  • Function

  • LoadVars

  • LocalConnection

  • Microphone

  • MovieClip

  • NetConnection

  • NetStream

  • Number

  • Object

  • SharedObject

  • Sound

  • String

  • TextField

  • TextFormat

  • Video

  • Void

  • XML

  • XMLNode

  • XMLSocket

It is a best practice to always declare what type of variable a function is actually returning. This especially helps Flash display appropriate error messages; note, however, that it is not required.

 function getProductName () :String {       Return this.productName; } 

Note that the datatype Void does not exist as a class but just as a type. All functions that do not return any other datatype, such as a string or a number, should be declared as Void , as in the following example:

 function setProductName (productName:String) :Void {       this.productName = productName; } 

As you continue to examine the new functionality of Flash MX 2004, you will see that the focus is on class-based development. As you will see, attaching code to timelines and object instances will result in less maintainable and less scaleable applications. Variable typing and building packages and classes will result in more manageable applications.

The ActionScript Editor

Flash MX Professional 2004 now has a built-in ActionScript editor that, when editing an ActionScript file, disables all panels, including the timeline, the tools, and the components. All the built-in error checking and auto-formatting functionality is built into the ActionScript editor. Every time an ActionScript file is opened in Professional, the ActionScript editor, as shown in Figure 1.3, opens.

Figure 1.3. Note the integrated ActionScript editor in Flash MX Professional 2004.

graphics/01fig03.jpg

 <  Day Day Up  >  


Object-Oriented Programming with ActionScript 2.0
Object-Oriented Programming with ActionScript 2.0
ISBN: 0735713804
EAN: 2147483647
Year: 2004
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net