Actionscript 3.0 for beginners: lesson # 4

Recall the example of our code of TicTacToe (found in full on Google Code ) and begin to analyze it in detail.

Import definitions

With Actionscript 3.0 has done a great job of cleaning and accommodation nell'alberatura classes (contained in the packages, package ) used during development. In other programming languages, like C for example, when you want to use a feature must explicitly "import" in the code. This operation of "import" is necessary to allow the compiler to have all the codes and definitions related to the features we want to use. ; Actionscript 3.0 mette a disposizione l'istruzione include che è identica all' #include del C/C++. In ActionScript 3.0, however, education import should not be confused with the equivalent C / C + + #include , Actionscript 3.0 provides education include that is identical to ' #include in C / C + +. The latter, in fact, "include" the actual code that, if used or not, the final executable is compiled. The statement import is the highest level and is more "intelligent" at compile time. It is used primarily for access classes without specifying their full name. In alre words instead of using forms such as:

1
flash.display . MovieClip = new flash.display . MovieClip ( ) ; var myMC: flash.display. MovieClip = new flash.display. MovieClip ();

By importing the definition of the specific package (or library if you prefer), we obtain the simpler and more readable:

1
2
3
. MovieClip ; import flash.display. MovieClip ;
/ /
MovieClip = new MovieClip ( ) ; var myMC: MovieClip = new MovieClip ();

In our example, the package declaration inside the class, we had three import :

1
2
3
.*; import flash.display .*;
.*; import flash.events .*;
.*; import flash.text .*;

. The first import makes available all the classes and the definitions in the package flash.display . Alternatively we could replace the entire package to the import (targeted) DEFINITION of each class and we use, namely:

1
2
3
4
5
. DisplayObject ; import flash.display. DisplayObject ;
. MovieClip ; import flash.display. MovieClip ;
. Stage ; import flash.display. Stage ;
. StageAlign ; import flash.display. StageAlign ;
. StageScaleMode ; import flash.display. StageScaleMode ;

This operation of "detail" is not a necessity for the final result. If an imported class is not used in the script, is not - intelligently - in the exported SWF file.

This means that you can import large packages without worrying about the size of the SWF file, bytecode associated with a class is included in a SWF file only if that class is actually used.

At most, if too far with "asterisks", ie with syntax like flash.* , we can have a "light" slowing down during the compilation! In "everyday life", therefore, you can use the syntax used with confidence in our example.

Constant static

Here is another interesting feature of Actionscript 3.0: static constants. In the example I introduced some way of example:

1
2
3
String = "TicTacToe" ; public static const NAME: String = "TicTacToe";
String = "1.0" ; public static const VERSION: String = "1.0";
String = "Giovambattista Fazioli <g.fazioli@undolog.com>" ; public static const AUTHOR: String = "Giovambattista Fazioli <g.fazioli@undolog.com>";

It is interesting to note that the same sequence can be written as:

1
2
3
String = "TicTacToe" ; public static const NAME: String = "TicTacToe";
String = "1.0" ; public static const VERSION: String = "1.0";
String = "Giovambattista Fazioli <g.fazioli@undolog.com>" ; public static const AUTHOR: String = "Giovambattista Fazioli <g.fazioli@undolog.com>";

Leaving aside these "curiosities" of the Flash CS3, we say that the static variables or constants are associated to the class and instance of the class! See Classes, Objects and Instances . This is an important difference, because in our case if we write - for example:

1
2
TicTacToe = new TicTacToe ( ) ; var ttt: TicTacToe = new TicTacToe ();
ttt . AUTHOR ) ; // errore trace (ttt. AUTHOR) / / error

This will generate an error. This is because the constants and static variables are directly associated with the definition of the class itself and not an instance. The correct way is:

1
TicTacToe . AUTHOR ) ; // Giovambattista Fazioli <g.fazioli@undolog.com> trace (TicTacToe. AUTHOR) / / Giovambattista Fazioli <g.fazioli@undolog.com>

The constant or static variables have the CHARACTER not be "instantiated" several times. They are used in situations where the stored value (either as a variable in the form of constant) remains valid for all instances of that class and then can be shared with all the "children".

Variables and Constants

It is easy to see the substantial difference between variable and constant, being the self-explanatory name. However, it should explain it because we just talked about. A variable is a space for a given (numeric, string, Boolean, etc ...) that can (and should) change its value during the execution of a code. In contrast the constants are spaces reserved for data that never change their value during the execution of a code: remain constant! See in the constants and variables: what is the real difference? .

In Actionscript 3.0, as in other languages, the constants can be set only once directly during or after the definition in the code:

1
2
3
4
5
6
7
8
9
10
11
class Example {
int = 0 ; COSTANTE_A public const: int = 0;
int ; COSTANTE_B public const: int ;
/ / Constructor
Esempio ( ) { public function Example () {
COSTANTE_B = 10;
}
}
Esempio = new Esempio ( ) ; var sample: Example = new Example ();
sample . COSTANTE_A ) ; // 0 trace (sample. COSTANTE_A) / / 0
sample . COSTANTE_B ) ; // 10 trace (sample. COSTANTE_B) / / 10

Next time we will continue the analysis of the code and we'll look at "types" given, interesting and important topic with the release of Actionscript 3.0.

3 comments to "Actionscript 3.0 for beginners: lesson # 4"

  1. February 18, 2009 NoCode:

    Would be better in English. This just does not make sense. Not very readable this way. Please write in English, publish in English. Or have a real translater translate it.

  2. February 18, 2009 NoCode:

    (Causes some Internet translater Is not good enough. I got a badly translated into Dutch site.)

  3. February 18, 2009 Giovambattista Fazioli :

    @ NoCode:
    I am sorry but It Would be too expensive for me to write in Italian and Italiano! : (

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA