One other thing occurred to me on this topic, that might actually save the trouble of using a hash table, which AS direly needs.

Is there a way to summon forth a class that's been instantiated in any of the ways described below, by name?

In other words, if I've done this:

for(var i:Number = 0; i < 12; i++) {
 Object.registerClass(symbolName,FooA);
var clip:FooA=FooA(myMovie.attachMovie (symbolName,"anInstanceName_"+i,depth));
}

And elsewhere, I want to summon forth these dynamically in another loop or otherwise (something I might normally do by stuffing the instances in a hash or map somewhere and using the name as the key) - can I do that in some fashion? Is there an AS idiom for obtaining a reference to a MovieClip (or other runtime object) by name?

Julian

On Jul 7, 2006, at 0:25 PDT, Ian Thomas wrote:

Hi Julian,
Now I've had my first cup of coffee I'll try to explain it more clearly. :-)

 In most cases AS2 works very similarly to Java in terms of
inheritance, instantiation and casting and the like - the only main
gotcha is that the class casting operator is MyClass(x) rather than
(MyClass)x - which looks suspiciously like a constructor and can
confuse the issue.

 The major difference is when we come to MovieClips, or
MovieClip-a-likes (TextField, for example). For historical reasons an
object which is of class MovieClip or an object which derives from
class MovieClip can't be instantiated directly via new - so new
MovieClip() or new FooA() (which extends MovieClip) will not work.
(Roll on AS3!)

 You can create a new, plain, vanilla MovieClip like so:

var clip:MovieClip=myMovie.createEmptyMovieClip(instanceName,depth);

You can create a MovieClip which represents a symbol in the library like so:
var clip:MovieClip=myMovie.attachMovie(symbolName,instanceName,depth);

Both of these operations dynamically create a new property,
'instanceName', on myMovie. If myMovie were a plain MovieClip (and
thus dynamically typed), you could type:

myMovie.createEmptyMovieClip("fred",depth);
myMovie.fred._visible=false; // do something to the clip
var clip:MovieClip=myMovie.fred; // grab a reference to the clip

(As an aside, if you're working with a non-dynamic class as the parent
movie clip i.e. something derived from MovieClip, the compiler
complains because it doesn't know about the variable fred - you can
type:
var clip:MovieClip=myMovie["fred"];
to get around this.)

To create an object associated with a symbol that is of a type derived
from MovieClip (e.g. FooA extends MovieClip) you can either:

 Set the class name in the linkage dialog of the library symbol.
var clip:FooA=FooA(myMovie.attachMovie (symbolName,instanceName,depth));
 (or
var clip:MovieClip=myMovie.attachMovie (symbolName,instanceName,depth);
 var foo:FooA=FooA(clip);
 It's the same thing)

or purely by code:

 Object.registerClass(symbolName,FooA);
var clip:FooA=FooA(myMovie.attachMovie (symbolName,instanceName,depth));

If you want to create an object which is derived from MovieClip that
is _not_ associated with a symbol instance (i.e. effectively an
emptyMovieClip with an associated class), you can do this arcane hack:

(For arguments sake, let's say FooA is actually com.misc.FooA which
extends MovieClip)
var symbolName:String="__Packages.com.misc.FooA";
Object.registerClass(symbolName,FooA);
var clip:FooA=FooA(myMovie.attachMovie (symbolName,instanceName,depth));

(The trick here is that for each class, Flash registers an 'invisible'
symbol with the name __Packages.<full class and package>)

I think that covers almost all you need to know about instantiating
MovieClips. :-)

I came from a Java background too (amongst other things), and it does
all sort of make logical sense, if you think of createEmptyMovieClip
and attachMovie as being object factories. But like I say, roll on
AS3, which gets rid of a lot of these historical peculiarities.

Hope that's more help,
 Ian


On 7/7/06, Julian Bleecker <[EMAIL PROTECTED]> wrote:
Ah, slowly getting it..I've dug in so deeply with Java idioms that
it's almost like being Charleton Heston in Planet of the Apes..

.julian.

Julian Bleecker, Ph.D.
http://research.techkwondo.com
[EMAIL PROTECTED]

http://training.figleaf.com

_______________________________________________
[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

_______________________________________________
[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

Reply via email to