Write good OO code in Adobe Flash

Here are some tips on how to write good code Object Oriented (OO) in Adobe Flash, especially for those still using version MX waiting to go to CS3.

Organize classes folders

First of all, the organization of classes makes the job significantly easier code maintenance. You can also create a real library you can reuse in other projects. Flash uses a classification related to the filesystem, and then organizing them into folders will also be reflected on the import of classes. For example, if we create the sequence of folders "mylibrary / graphics / plot" and insert our own ActionScript class "PlotClass.as", when we should use to import the class:

1
grafica . plot . PlotClass ; mylibrary imports. graphics. plot. PlotClass;

If the library (folder) "MyLibrary" is not in your movie folder or project, use the Flash publish settings to select the path:

Setting percoroso libraries


Encapsulate the code: using the properties

Haste is always "bad advisor", so you should spend more time in order to reap the fruits of our efforts later. When developing a class in ActionScript tends to "show" to all our own, even those that are actually used only internally to the class.

1
2
3
4
class MyClass {
Number = 10 ; var mio_valore: Number = 10;
function MyClass () {}
}

All this is bad because it makes our code extrema unstable and prone to error, even if we are to use the objects in question. The rule allows dell'incapsulmento instead of controlling the setting properties of a class extremely rendondola more stable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MyClass {
__mio_valore : Number = 10 ; __mio_valore private var: Number = 10;
function MyClass () {}
get MioValore ( ) : Number { MioValore public function get (): Number {
__mio_valore ) ; return (__mio_valore);
}
set MioValore ( v : Number ) { MioValore public function set (v: Number ) {
/ / Pssst: using the ternary operator ... ;)
/ / __mio_valore = (V> 5 & & v <20)? V: __mio_valore;
v > 5 && v < 20 ) { if (v> 5 & & v <20) {
__mio_valore = v;
}
}
}

in modo tale che possa assumere solo i valori compresi tra 5 e 20. In this example the property MioValore , as seen from the outside, is controlled by the function set so that it can only take on values ​​between 5 and 20. se preferite. This means that within our class we will refer only to __mio_valore or this.__mio_valore if you prefer. However we will always be sure that the "variable" __mio_valore will be between 5 and 20, unless we are ourselves, within the class, change the value incorrectly.
, permette di creare proprietà in sola lettura, in sola scrittura o entrambe, il chè non è da poco. Moreover, the use of the property, or the function get and set , you can create read-only property, write-only, or both, which is second to short.

Private and Public Use when you need it

The methods used only internally to the class should not be exposed outside. First of all because they do not need it! Second, because in fact not part of the interface object. When you release a version of a class is implicitly certifies that the methods and properties that the class exposes (the interface) will be maintained in future releases of the object. Obviously that can change is their implementation, ovvvero the underlying code, but not the name or the parameters of our method or property. Using the methods private , however, these will be visible only from the inside of the object. We could use them and, if necessary, modify the whole, without violating the fundamental rule of object oriented programming.

Inherit

The inheritance (inheritance), which is the characteristic of an object to be descended from one another and then "inherit" all the characteristics of the object "mother", is a unique tool of object oriented programming. The construction of hierarchies, however, requires much experience, so as not to create unnecessary code subsets that make a huge elephant soon unmanageable. Often the classes are created motherboard (IUnknown) after a long analysis or after you noticed some "redundancy" when creating classes.
But the fact remains that to use it with caution, can extend in a few seconds, all objects of a class filgi. ActionScript uses the keyword extends to create inheritance between a class and another.

1
2
3
4
5
6
7
8
9
10
11
12
/ / File ClasseMadre.as
{class ClasseMadre
ClasseMadre function () {}
Addiziona ( a : Number , b : Numer ) : Number { Adds public function (a: Number , b: Number): Number {
a + b ) ; return (a + b);
}
}
/ / File Figlio.as
ClasseMadre imports;
ClasseMadre { {class Son extends ClasseMadre
Son function () {}
}

, ereditato appunto dalla classe madre ClasseMadre . In this example the class Figlio of the method has automatically Addiziona() , in fact inherited from the parent class ClasseMadre . Especially useful is the ability to always refer to the parent class using the keyword super , so as to allow the overwrite inherited methods and properties.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/ / File ClasseMadre.as
{class ClasseMadre
ClasseMadre function () {}
Addiziona ( a : Number , b : Numer ) : Number { Adds public function (a: Number , b: Number): Number {
a + b ) ; return (a + b);
}
}
/ / File Figlio.as
ClasseMadre imports;
ClasseMadre { {class Son extends ClasseMadre
Son function () {}
Addiziona ( a : Number , b : Numer , c : Number ) : Number { Adds public function (a: Number , b: Number, c: Number ): Number {
super . Addiziona ( a , b ) + c ) ; return (super. Adds (a, b) + c);
}
}

This, in my opinion, is one of the many beauties of the object development!

Solutions

If the class we are writing is linked to a MovieClip, effectively making them an extension, requires that any ActionScript object (MovieClip or TextField) contained in the MovieClip father, is referenced at the beginning of the class. For example, if we extend a MovieClip It contains a second MovieClip secondo_mc identified by name, when we declare the class must include a reference to this MovieClip in this simple way:

1
2
3
4
5
6
7
8
9
MovieClip { class MyClass extends MovieClip {
secondo_mc : MovieClip ; private var secondo_mc: MovieClip ;
function MyClass () {}
/ /
Refresh ( ) { public function refresh () {
/ / ActionScript would fail if we had not declared this variable / pointer to the beginning
secondo_mc . _x = 10 ; this. secondo_mc. _x = 10;
}
}

When the objects to be manipulated is known there is no problem. However, you may want to create a MovieClip runtime and is therefore not expected to have no line of the original declaration. For example, this code will not work:

1
2
3
4
5
6
7
8
9
MovieClip { class MyClass extends MovieClip {
function MyClass () {}
/ /
CreaMovie ( ) { CreaMovie public function () {
, 100 ) ; createEmptyMovieClip ("secondo_mc", 100);
/ / ActionScript does not know this object
secondo_mc . _x = 10 ; this. secondo_mc. _x = 10;
}
}

This is a very common case, imagine the following code:

1
2
3
4
5
6
7
8
9
MovieClip { class MyClass extends MovieClip {
function MyClass () {}
/ /
CreaMovie ( ) { CreaMovie public function () {
var i = 0 ; i < 50 ; i ++ ) { for (var i = 0; i <50; i + +) {
+ i , i ) ; createEmptyMovieClip ("item" + i, i);
}
}
}

In this case, as in others, it is possible to access an object as an index of an array, ie:

1
2
3
4
5
6
7
8
9
10
MovieClip { class MyClass extends MovieClip {
function MyClass () {}
/ /
CreaMovie ( ) { CreaMovie public function () {
var i = 0 ; i < 50 ; i ++ ) { for (var i = 0; i <50; i + +) {
+ i , i ) ; createEmptyMovieClip ("item" + i, i);
"item" + i ] . _x = 10 ; this ["item" + i]. _x = 10;
}
}
}

This is a technique very useful in many circumstances.

Versioning

One last useful thing I suggest is the versioning, that is sure to keep a record of changes. When you are dealing with a series of these classes tend to evolve over time. Good rule of thumb is therefore to adopt a program (SCCS [Source Code Control System], CVS, Subversion for MacOS / Linux / Unix or FileHamster under Windows) can maintain versioning of files of the various releases that we issue.
In addition to commenting on the code and the versions / revisions of the board set a variable within the class (style JavaScript) in order to make "speaking" the same object code:

1
2
3
4
class MyClass {
__release : String = "1.0" ; __release private var: String = "1.0";
function MyClass () {}
}

Inclusions and constant

You can include code directly in the classroom. Profit at the organizational level, type:

1
2
3
MovieClip { class MyClass extends MovieClip {
# Include "ClassVersion.as"
}

If you lack the #define (really a shame that they are not supported!) of C, here is a solution pretty decororsa:

1
2
3
4
5
6
7
8
ERROR_1 : String = "Errore tipo 1" ; public static var ERROR_1: String = "Error type 1";
/ / Or - code by Adobe
/ / Values ​​for command types for _cmdQueue
Number = 0 ; static var PLAY: Number = 0;
Number = 1 ; static var LOAD: Number = 1;
Number = 2 ; static var PAUSE: Number = 2;
Number = 3 ; static var STOP: Number = 3;
Number = 4 ; static var SEEK: Number = 4;

One Response to "Write good OO code in Adobe Flash"

  1. September 3, 2008 Actionscript 3.0 for beginners: lesson # 2 | Undolog.com :

    [...] Public properties, using the encapsulation of its OO programming (see write good OO code in Adobe Flash). __player __stop and denote the active player and the state of [...]

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