Unless I'm totally misunderstanding your code, that is kinda nutty. :-)

As far as I can see, you're casting loader.content to contentClass so
that startUp() won't throw a compile error?

However, the answer you've come up with is a false solution, as the
code doesn't know what class contentClass is until runtime. Flash is
throwing away the type information on your contentClass() cast,
because it doesn't know during compile what contentClass is.

You could just as well say
contentClass(loader.content).callMissingMethod(); and it would still
compile.

You could achieve exactly the same effect by using either of these techniques:

private function onInit(e:Event):void
{
  addChild(loader);
  (loader.content as Object).startUp(); // Casting to object throws
away type safety.
}

or

private function onInit(e:Event):void
{
  addChild(loader);
  loader.content['startUp']();
}

Extracting the class using getClassDefinition is actually doing
nothing useful in your code here.

*tries to think of a way to explain it*

I'll try to break it down...

getQualifiedClassName(loader.content) gives you the class name of
loader.content at runtime. Say com.app.MyClass;

getDefinitionByName("com.app.MyClass") gives you the Class object of
loader.content at runtime.

dynamicClassVar(loader.content) is you trying to tell the compiler to
cast loader.content to whatever class is contained in dynamicClassVar.
However, a cast is an instruction to the _compiler_ . We're already
well past the compiler, because we're dealing with runtime objects.
The compiler throws away the cast and any compile-time type-checking
on loader.content because it doesn't yet know what dynamicClassVar is.

So:

class SomeClass extends Sprite
{
  public function method():void
  {
  }
}

var obj:Sprite=new SomeClass();
obj.method(); /// Compile error - compiler doesn't know obj is type SomeClass
SomeClass(obj).method(); // Works fine
SomeClass(obj).monkey(); // Compiler error - SomeClass doesn't have a
method monkey()

var myClass:Class=SomeClass;
myClass(obj).method(); // Works fine _BUT_ compile-time type-checking
thrown away, because compiler doesn't know what type of class myClass
contains.
myClass(obj).monkey(); // Compiles fine but throws runtime error.

Does that make any sense at all, or am I just really bad at explaining things?

Ian

On Fri, Nov 21, 2008 at 12:14 AM, Joel Stransky <[EMAIL PROTECTED]> wrote:
> This may be considered kinda nutty but I use this to dynamically detect
> Document classes in loaded .swfs.
>
>        loader.contentLoaderInfo.addEventListener(Event.INIT, onInit);
>
>        private function onInit(e:Event):void
>        {
>            addChild(loader);
>            contentClass =
> getDefinitionByName(getQualifiedClassName(loader.content));
>            contentClass(loader.content).startUp();
>        }
>
> On Thu, Nov 20, 2008 at 5:48 PM, Ian Thomas <[EMAIL PROTECTED]> wrote:
>
>> To be honest, I don't know - but I suspect
>> flash.utils.getDefinitionByName() is an alias for
>> ApplicationDomain.currentDomain.getDefinition(). Whereas you can also
>> use getDefinition() to retrieve class definitions from within other
>> ApplicationDomain objects - such as loaded Flex modules.
>>
>> Essentially ApplicationDomains are all about code segregation. You
>> remember how in AS2 if you loaded in a second SWF none of its class
>> definitions would overwrite the ones in the loading movie i.e. it
>> 'inherited' the classes of the parent? i.e. if you had a class called
>> myapp.ClassA in both the movie doing the loading and the movie getting
>> loaded, the first definition encountered would be the one that won. A
>> pain if the movie you're loading in was compiled later, say, with a
>> different version of ClassA.
>>
>> In AS3 in the flash.display.Loader class (or Flex ModuleManager or
>> various other loading classes) you can specify different
>> ApplicationDomains to use for the .swf you're loading in. It's almost
>> like namespacing.
>>
>> If you pass null i.e. the default, from memory that means that the
>> loading .swf gets loaded into a _child_ ApplicationDomain i.e. gets
>> all the parent classes, but none of the new classes are directly
>> instantiable in the parent movie. From memory again, I think that's
>> the same as saying new
>> ApplicationDomain(ApplicationDomain.currentDomain), the latter
>> creating a child domain of the current one.
>>
>> If you pass ApplicationDomain.currentDomain, the loading .swf gets all
>> the parents classes and the new classes it loads are instantiable in
>> the parent movie.
>>
>> If you pass new ApplicationDomain(), the loading .swf is completely
>> isolated. It has its own versions of all classes, and won't
>> accidentally inherit definitions from the parent.
>>
>> This is just for the purposes of direct use of the class/package name
>> i.e. new com.mypack.ClassA(). However, you can get to different class
>> definitions (even different definitions of class with the same
>> name/package as each other) by using getDefinition() on the
>> appropriate ApplicationDomain.
>>
>> I hope that makes sense!
>>
>> My only irritation with it - currently - is that the Flex framework
>> doesn't support running inside a completely separate
>> ApplicationDomain. So loading a Flex .swf into a self-contained
>> ApplicationDomain just doesn't work; which is a shame, as it would
>> have meant that running a Flex 2 app inside a Flex 3 app was perfectly
>> viable or vice versa (useful for legacy; also useful for the project
>> I'm working on at the moment which has one set of CSS in the parent
>> app, a different set in the child...). Apparently that's on the cards
>> to be fixed for Gumbo.
>>
>> That was probably far more detail than you wanted. :-D
>>
>> Ian
>>
>> On Thu, Nov 20, 2008 at 10:26 PM, Merrill, Jason
>> <[EMAIL PROTECTED]> wrote:
>> > Ian and David - interesting. So what is the difference between
>> getDefinition and getDefinitionByName?
>> >
>> > What I ended up using was this, which works well:
>> >
>> > var ClassReference:Class =
>> getDefinitionByName("people."+_associates[_associateCount]) as Class;
>> > var instance = new ClassReference();
>> >
>> > If I need it to be cast as the class it subclasses, like a MovieClip, so
>> it can be added to the stage, I do:
>> >
>> > addChild(MovieClip(instance));
>> >
>> > is there any particular reason to use getDefinition instead?  At first
>> glance, seems to accomplish the same thing.
>> >
>> > Jason Merrill
>> > Bank of America     Instructional Technology & Media   ·   GCIB & Staff
>> Support L&LD
>> >
>> > Interested in Flash Platform technologies?  Join the Bank of America
>> Flash Platform Developer Community
>> > Interested in innovative ideas in Learning?  Check out the Innovative
>> Learning Blog and subscribe.
>> >
>> >
>> >
>> >
>> >
>> >
>> > -----Original Message-----
>> > From: [EMAIL PROTECTED] [mailto:
>> [EMAIL PROTECTED] On Behalf Of David Hershberger
>> > Sent: Thursday, November 20, 2008 5:22 PM
>> > To: Flash Coders List
>> > Subject: Re: [Flashcoders] get class by definition - or something like
>> that
>> >
>> > You want ApplicationDomain.getDefinition(className: String)
>> > and also ApplicationDomain.hasDefinition(className: String) is handy.
>> >
>> > Dave
>> >
>> > On Thu, Nov 20, 2008 at 1:52 PM, Merrill, Jason <
>> > [EMAIL PROTECTED]> wrote:
>> >
>> >> Refersh my memory.  What was the name of that AS3 class method to get a
>> >> class definition from a string? Something like
>> >> getObjectByDefinition("MyClass") or something like that, but that's not
>> it.
>> >>  Our proxy server is down and I can't Google it, and the help docs are
>> no
>> >> help.
>> >>
>> >> Basically, I'm tring to remember the AS3 method to dynamically
>> instantiate
>> >> a class from a string definition - i.e.
>> >>
>> >> var ClassDefinition:Class = getClassByDefinition("Apple");
>> >> var newClassInstance:* = new ClassDefinition();
>> >>
>> >> Something like that, I've done it before, I just can't find the syntax
>> or
>> >> remember the method.
>> >>
>> >> Jason Merrill
>> >> Bank of America     Instructional Technology & Media   ·   GCIB & Staff
>> >> Support L&LD
>> >>
>> >> Interested in Flash Platform technologies?  Join the Bank of America
>> Flash
>> >> Platform Developer Community
>> >> Interested in innovative ideas in Learning?  Check out the Innovative
>> >> Learning Blog and subscribe.
>> >>
>> >>
>> >> _______________________________________________
>> >> Flashcoders mailing list
>> >> Flashcoders@chattyfig.figleaf.com
>> >> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>> >>
>> > _______________________________________________
>> > Flashcoders mailing list
>> > Flashcoders@chattyfig.figleaf.com
>> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>> >
>> > _______________________________________________
>> > Flashcoders mailing list
>> > Flashcoders@chattyfig.figleaf.com
>> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>> >
>>
>> _______________________________________________
>> Flashcoders mailing list
>> Flashcoders@chattyfig.figleaf.com
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>
>
>
> --
> --Joel Stransky
> stranskydesign.com
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to