1. Load/init ordering: When do classes load, and when do static
initializers execute? If a class has a static property that is
assigned outside a constructor, is this initialized when the class
loads (presumably frame 1?) or the first time it is accessed, or what?
If class A refers to a static property of class B, how do I ensure
that class B is initialized before class A? What if each class refers
to a static property of the other class? (These are somewhat silly
examples to illustrate the grey area I'm trying to root out.)
I believe classes load on whatever frame they are set to export to
(under Actionscript 2 in Publish Settings). The default is frame 1
but you can set a different frame. Other properties of the class
initialize when the class is instantiated; as to specific order for
your situation, I would test with a series of trace statements.
2. Timeline/clip context: I am looking for corrections to a few
asserstions here:
- "this" (always?) refers to a movie clip when the code is outside any
class; often the main movie when in the movie's timeline.
- "this" refers to an object when inside any class method, with one
exception, which is when defining a function within a method, like so:
function classMethod() {
var loader = new loadVars();
loader.onData = function(text) {
this.foo(); /// "this" here does not refer to our class, but as
far as I can tell, to nothing
}
}
- "this" is dynamic; within the same function, it may take on
different meanings at run time depending on where the function is
called. (In which case I'm fuzzy on when and what.)
In general, 'this' refers to the object of current scope. When used
in a class, it refers to the class; when used on a frame in the Flash
editor, it refers to the current timeline; when used in a callback,
as in:
foo_mc.onRollOver = function() {
trace(this._x);
}
it refers to the object that owns the callback. In your example
above, 'this' refers to your 'loader' variable, as it occurs in the
onData function, which is a method of the loader object.
3. Associating MovieClips and classes: Can a class simply extend
MovieClip? And what exactly is happening when a library symbol is
linked to a class, at runtime?
You can indeed extend MovieClip. However, I often find it better to
create movieclip-related classes by composition rather than extension.
To link a class to an mc, you need to do two things:
1. your class must extend MovieClip
2. Your library symbol must be linked to the class name via its
Linkage... panel in the Flash editor.
You can create instances of this sort of class in two ways: by
dragging one onto the stage, or by doing an attachMovie() on the
class symbol's linkage ID.
I tend to favor composition regardless, but there is something ugly to
me about having multiple classes which logically correspond to movie
clips in the library having to hard-code attachMovie(), which also
introduces an architectural problem that seems to break encapsulation.
If I have, for instance, 3 classes that handle different aspects of a
GUI, or perhaps sprites, and each class in its constructor uses
attachMovie() to create graphics from library symbols, if they all
attach to _root, then they must make sure to each use distinct depths,
which forces each class to know what depths the others are using (or
*always* using _root.getNextHighestDepth(), which isn't always
desirable).
First, the attachMovie business is indeed kind of kludgey; why can't
I just do
var foo:MovieClip = new MovieClip();
But that's Flash for you. If you like this sort of setup, you can use
movieclips via composition and do:
var foo:MyClass = new MyClass();
and have MyClass handle the attachMovie() and depth setting jazz. But
even so, MyClass will have to know what depth to create its clips at,
and what movieclip to attach them to, so you'll probably have to pass
them in to MyClass as parameters when you instantiate it.
Alternatively, I could place container clips on the stage
at author time corresponding to each class, so depths are segregated
in this way, but this feels like its adding unnecessary complexity.
One way to tackle the depth management problem is to have each class
have its own base movieclip, and attach its sub-clips to that base
clip. For example, if your app has, say, three UI panels, each panel
could have its own base clip (maybe created by createEmptyMovieClip()
in your class file). Then each panel class only needs to keep track
of its own clips, and your main application only needs to keep track
of the panel clips. The depths for each are thus encapsulated in the
appropriate class.
Finally, if a class uses a singleton pattern, where it has one and
only one static instance of itself, but also requires a movie clip, I
am confused as to when this instance gets added to the main timeline,
which then affects depth stacking order (see question #1).
Singletons are instantiated like any other class, and you can set the
depth however you wish at the time you instantiate it.
You sound like you have a good deal of programming experience but you
might be new to Flash. Which Flash reference books do you have? I
have gotten a lot out of Colin Moock's O'Reilly books:
ActionScript for Flash MX, the Definitive Guide (excellent language
reference)
Essential Actionscript 2 (good OO book and good examples of basic
design patterns including Singleton, Observer, and MVC)
Also, the site http://actionscripts.org has a lot of excellent
tutorials and articles for all levels of Flash developers.
Hope this helps. Others on the list, please correct me if I have mis-
stated any of the above.
Best regards,
OK
DAH
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com