Hello people,
In my opinion we have 2 valid and concrete ways to initiate another class from the Document Class (Main Class).
1: We write the Class to initiate exactly like the Document Class.
2: We write a Class in the same AS file of the Document Class, but outside the package.
There would be other ways, like for example taking advantage or the inheritance of the Document Class, but I?d enter a subject that I don?t want to cite in this article, so I?ll postpone it to future articles.
A concrete example ( so that nobody thinks we only talk here ), I create an FLA?
and I save it as ?chiamate.fla?. I create an AS file and save it as ?Prima.as?. I define the Prima class:
where you can see I?ve also defined another class called Terza, still in the same file, but outside the package.Code:package { import flash.display.MovieClip; public class Prima extends MovieClip { private var la_seconda:Seconda; public function Prima() { la_seconda=new Seconda(); var la_terza:Terza=new Terza(); } } } import flash.display.MovieClip; class Terza extends MovieClip { public function Terza() { trace('La classe Terza è stata istanziata'); } }
Now I create another AS file, I save it as ?Seconda.as? in the same folder where both the FLA and Prima.as and I define the Seconda class this way (exactly like Prima-as):
These are 2 ways to initiate other classes from the Main Class, exactly like a pure OOP application.Code:package { import flash.display.MovieClip; public class Seconda extends MovieClip { public function Seconda() { trace('La classe seconda è stata istanziata'); } } }
It?s the best way to start our Flash application.
I?d like to give you a citation by Colin Moock ( Actionscript architect ) who I?ve always appreciated and whose advice I follow:
?In a pure Object Oriented Flash application, a FLA file might contain only a single frame, which simply loads the application's main class and starts the application by invoking a method on that Main Class.?
This way we?ll have total control of our classes and their methods and properties, but more than anything we?ll have an application with only one frame and completely modular.
Analyzing the 2 classes I?ve written, you can notice that the Seconda class is initialized as a property of the Main Class, while the Terza class is instantiated with a local variable inside of the constructor ( see the article ?How to create a class in Actionscript 3.0? to know more about the constructor ) of the Main Class, so being a local variable, as it?s born it dies and can?t be recalled ( in this case only within the execution of the constructor code).
Have fun !
Bookmarks