> all classes are created when the application is started
AS3 classes get initialized -- meaning that their static variables get
set and their static methods get executed -- in a lazy fashion, as they
are used. If you have bytecode for a class in your SWF and that class
doesn't get used by other code, that class will never get initialized.
On the other hand, *instances* of DisplayObject classes get created "top
down" when you use MXML. The Player automatically creates an instance of
SystemManager. The instance of SystemManager automatically creates an
instance of MainClass as its child. The instance of MainClass creates
Class2 as its child. Etc. MXML, with its tags nested inside other tags,
is essentially shorthand. For example,
<Button id="b" label="OK"/>
is basically shorthand for
public var b:Button;
...
// inside some method:
b = new Button();
b.label = "OK";
addChild(b);
It sounds like you might want to experiment with dynamically creating
class instances of DisplayObjects like this with 'new' -- and attaching
them to parents with addChild() and detatching them with removeChild()
-- in order to have more of a correspondence with what you're familiar
with, namely C++. It might be that the MXML syntax, which hides the
instantiation details, is confusing you.
> In C++ I would initialize the classes I wanted to use by using calling
the class initializer().
I think you mean that you would create the instances you wanted by using
the 'new' operator, which invokes the class's constructor.
> if I needed to access a private variable from MainClass.mxml I
included the header file (in this case the class4.mxml file)
There are no header files in AS3. Instead of 'include'ing a header file,
you import a class. But if something is private you can't access it from
any other class.
> Also in C++ when I'm done with the instance I needed to call the
destructor to shut the class down and free the memory
> I haven't seen a reference yet to destroy a class instances in
Flex/AS3
A C++ destructor doesn't shut a class down; it gets called when an
*instance* is freed with the 'delete' operator. You don't have to delete
instances in AS3 because they get "garbage collected". Periodically the
runtime determines whether there are any references to an instance. If
not, it frees it. There is no method similar to a destructor that gets
called when this happens.
> I've gotten confused on how long those class instances live and when
exactly they are initialized.
In the case you're describing, they live until you quit the app. If you
did something like use removeChild() to detach the instance of Class3
from the instance of Class2, then the instances of Class3 and Class4
would become subject to garbage collection. (Which doesn't necessarily
ensure that they'll actually get garbage collected unless the Player
decides it is running low on memory.) They were initialized by methods
of framework core classes, such as Container and SystemManager, in the
top-down order that I previously described.
> When I use the debugger it doesn't look like in the above example that
class4.mxml stays active if I leave the Class3.mxml code.
I'm not clear on what you're seeing, but it sounds like you're
misinterpreting it.
Gordon Smith
Adobe Flex SDK Team
________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Scott
Sent: Saturday, July 26, 2008 4:16 PM
To: [email protected]
Subject: RE: [flexcoders] mxml components
I was afraid of that...
Ok, let's see if I can do it better.
I understand that I'm not "calling" a class, but functions within the
instance.
So if I understand the other message from Gordon correctly then all
classes are created when the application is started. However, it
doesn't look like that in the debugger when I'm running the application.
Let's take and expand my question example below...
MainClass.mxml
|
|-----Class2.mxml - ShowStat():Boolean
|
|-----Class3.mxml - LoggedIn():Boolean
| |--Class4.mxml - LogMeIn(strName:String,strPassword:String)
|-- private var bIsLoggedOn:Boolean
(This is fictitious for point of discussion)
If I control the display and how things are laid out in MainClass.mxml,
I used the Class2 and Class3.mxml files as subclasses to control the
widgets on the screen being placed by the MainClass.mxml.
In C++ I would initialize the classes I wanted to use by using calling
the class initializer(). (I have been using the
creationComplete="init()" to start code in the class instance.) Then if
I needed to access a private variable from MainClass.mxml I included the
header file (in this case the class4.mxml file) and called the function,
in the above example LoggedIn():Boolean. Also in C++ when I'm done with
the instance I needed to call the destructor to shut the class down and
free the memory. I haven't seen a reference yet to destroy a class
instances in Flex/AS3.
While using the individual "class".mxml files I've been created, I've
gotten confused on how long those class instances live and when exactly
they are initialized. And how do I "destroy" the class instances and
the variables they contain to free memory (is this necessary in
Flex/AS3)? When I use the debugger it doesn't look like in the above
example that class4.mxml stays active if I leave the Class3.mxml code.
Finally, if I had a reason to log the user in from another area in the
code; say from MainClass.mxml, if I wanted to call LogMeIn( myusername,
mypassword) from Class4.mxml I can't tell if I'm creating a new instance
of that class or am I using the existing instance? In other words...:
Are these the same instance or unique instances with in each of the
class instances themselves (mainclass/class3)?
MainClass.mxml:
<mx:script>
<![CDATA{
Import com.myapp.models.LoginClass;
...
..
And
Class3.mxml
<mx:script>
<![CDATA{
Import com.myapp.models.LoginClass;
...
..
________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Tracy Spratt
Sent: Friday, July 25, 2008 5:07 PM
To: [email protected]
Subject: {Disarmed} RE: [flexcoders] mxml components
First, there are "classes" and "instances". When it matters, use these
terms correctly.
Second, "call" does not apply to a "class", or even really an
"instance".
An instantiated class stays instantiated untill all references to it are
removed, and "Garbage Collection" has run and removed it.
If this is not the answer, perhaps you can rephrase your question,
without using the word "call"?
Tracy
________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Scott
Sent: Friday, July 25, 2008 5:59 PM
To: [email protected]
Subject: [flexcoders] mxml components
Ok, I'm trying to get a firm grasp on AS3 components/classes. I'm
trying to figure out how to structure code so I can optimize code
re-use.
What is the life of a component/class in a flex application?
In other words...
If I have a main.mxml that calls another class like logins.mxml, is that
class in existence only while it's called? Once the event hits to call
it back is the class still alive? It looks like it kills it from memory
but I'm not 100% sure.
Take the following examples...
MainClass.mxml
|
|--------subclass1.mxml
|
|--------subclass2.mxml
| |----subclass2a.mxml
If MailClass.mxml calls subclass1.mxml, completes and then calls
subclass2.mxml which calls subclass2a.mxml, what are the lives of these
classes?
Also, do I need to be worrying about cleanup or is that handled
automatically?
I hope I'm explaining this well....
--
This message has been scanned for viruses and
dangerous content by MailScanner <http://www.mailscanner.info/> , and is
believed to be clean.