strange type casting behavior - accessing and type casting loaded objects in different classes

5 views
Skip to first unread message

jpso...@gmail.com

unread,
Mar 19, 2008, 6:06:47 PM3/19/08
to BulkLoader users
I am trying to load items in one class and them access them in
another. Once I retrieve the loaded object using getContent() I try to
type cast it to the class I want to use. Oddly, type casting only
seems to work if I do it in the class that created the BulkLoader.

Actually, I was able to get it to work if, and this is odd, I make a
reference to the loaded object in the class that created the
BulkLoader. I don't have to type cast it in that class...I just need
to getContent(). Then, in other classes that refer to the BulkLoader
via BulkLoader.getLoader(loaderName), I can type cast the object.

Here is what I'm doing:

public class LoaderApp
{

private var bulkLoader:BulkLoader;

private function loadItems():void
{
bulkLoader = new BulkLoader("AssetLoader");
bulkLoader.add("myCustomClass1.swf");
bulkLoader.add("myCustomClass2.swf");
bulkLoader.addEventListener(BulkLoader.COMPLETE,
handleItemsLoaded);
}

private function handleItemsLoaded():void
{
var myCustomClass1:CustomClass1 =
bulkLoader.getContent("myCustomClass1.swf") as CustomClass1;
// If I add the next line type casting will work in later objects
referencing the loaded content
// var myCustomClass2:Object =
bulkLoader.getContent("myCustomClass2.swf");
myCustomClass1.init();
}

}



public class myCustomClass1
{
public function init():void
{
// This works if left untyped
var untypedObject =
bulkLoader.getLoader("AssetLoader").getContent("myCustomClass2.swf");

// THIS FAILS unless I referred to this object in the class the
created the BulkLoader. Why?
var myCustomClass2:CustomClass2 = untypedObject as CustomClass2
}
}


Any ideas on what is happening?

Let me know if you need more info.

Thanks!

jpso...@gmail.com

unread,
Mar 19, 2008, 6:13:16 PM3/19/08
to BulkLoader users
Correction, I actually do have to type cast the loaded object in the
class that created the BulkLoader in order to type cast that object
later in another class.

So, I revised this method (still not what I want though because that
means that the class creating the BulkLoader needs to know about every
class implemented by the loaded swfs)

private function handleItemsLoaded():void
{
var myCustomClass1:CustomClass1 =
bulkLoader.getContent("myCustomClass1.swf") as CustomClass1;


// !!! This next line changed from my original post. Item must be
type cast in object creating the BulkLoader

var myCustomClass2:CustomClass2=
bulkLoader.getContent("myCustomClass2.swf") as CustomClass2;


myCustomClass1.init();

}

Arthur Debert

unread,
Mar 19, 2008, 6:15:45 PM3/19/08
to bulkload...@googlegroups.com
Hi There.

No idea, actually, but it is not something related to BulkLoader.

My number one guess would be: the compiler is not including the class,
unless you tell it specifically to do so with the first class
specifically calling it (it then forces that class to be included in
the swf).

Cheers
Arthur

Arthur Debert

unread,
Mar 19, 2008, 6:23:26 PM3/19/08
to bulkload...@googlegroups.com
Yep.

This is a common issue in as3 (and not related to BulkLoader). In
short you will have to either:
a) forget about strong typing on the loading swf
b) Use interfaces instead of concrete types and include those in both
c) use flex builder and specify them as library only (only used for
compile time checking, but doesn't compile it into the swf)

This situation more complex than it seems at first glance, a good
reading on it are Jesse Warden's posts on it (for some reason his site
seems to be offline).

Cheers
Arthur Debert

jpso...@gmail.com

unread,
Mar 19, 2008, 7:55:25 PM3/19/08
to BulkLoader users
I've read Jesse Warden's posts on the subject and have been was able
to deal with this issue before using the BulkLoader. I am using
interfaces, but I don't understand what is going on when using the
BulkLoader. Why is accessing the loaded swf different depending on how
I access the BulkLoader? In other words, why does the class that
instantiates the BulkLoader have the ability to type cast the loaded
swf while the class accessing the BulkLoader using the static
getLoader method fail when type casting?

jpso...@gmail.com

unread,
Mar 19, 2008, 8:59:05 PM3/19/08
to BulkLoader users
So, if I have this right, the object loading the swf has to have the
class dependencies of the type for which you want to cast the loaded
swf.

If swfA has BulkLoader in it, then when it wants to type cast an
object from BulkLoader to ISomeInterface they know they are using the
same ISomeInterface because the class dependencies are in the swf.

But, when a loaded swf, swfB, wants to type cast an object from
BulkLoader to ISomeInterface, because ISomeInterface is not in the swf
for which BulkLoader was compiled there is no guarantee that they are
using the same ISomeInterface. Basically, as far as the compiled
BulkLoader is concerned, there is no ISomeInterface. So, an object
from swfA (via BulkLoader) does not have the ability to ever be an
ISomeInterface.

Type casting loading objects is fun ;o)

I have to rethink my strategy for handling this. I would like to:
* Load all objects needed to begin the app at once
* Use strong typing
* Break the app into modular pieces and limit class dependencies (so
having one class know about all interfaces used in the app is not an
option)

There has to be a way to do this.

Thanks for your quick replies.

-JP

jpso...@gmail.com

unread,
Mar 20, 2008, 5:28:19 PM3/20/08
to BulkLoader users
Okay, I've done a bit of testing to try and figure out what is going
on.

First, you are right, it has nothing to do with the BulkLoader.

Your #1 guess is right on.

Basically, if swfA loads both swfB and swfC, then swfB can't type cast
swfC unless swfA has a reference to the desired type. swfB and swfC
can only refer to each others types if those types are in swfA. You
don't have to type cast swfB or swfC within swfA, you just need a
reference to the desired type in swfA (so that the class dependencies
are in swfA). Note, simply importing the type will add the
dependency...you need to use it somewhere.

I tried this without using BulkLoader and had the same results.

Just wanted to follow up.

jpso...@gmail.com

unread,
Mar 20, 2008, 5:45:17 PM3/20/08
to BulkLoader users
HOLD THE PHONE! I think I figured out how to get the type casting I
want. In my example without BulkLoader I specified the
ApplicationDomain and it worked:

ldr = new Loader();
var context:LoaderContext = new LoaderContext(false,
ApplicationDomain.currentDomain);
ldr.load(new URLRequest(assetURL), context);

Now swfA does not have a reference to the desired types of swfB or
swfC but swfB and swfC can successfully type cast each other!

I think I read that you provide something for setting the
ApplicationDomain for BulkLoader. Now to find that...

Arthur Debert

unread,
Mar 20, 2008, 5:54:36 PM3/20/08
to bulkload...@googlegroups.com
Hi There.

Glad you got this sorted out.

Sharing code between several swfs in AVM2 is a bit trickier, and
that's why we provide for a "context" option to swf items, so that
ApplicationDomain can be specified.

Cheers
Arthur Debert

jpso...@gmail.com

unread,
Mar 20, 2008, 6:06:54 PM3/20/08
to BulkLoader users
This is great! The problem is solved by using the "context" option!

So now I have something like this and it works great (as in I can type
cast as I need):

var thecontext:LoaderContext = new LoaderContext(false,
ApplicationDomain.currentDomain);
bulkLoader.add(assetURL, {context: thecontext});

Now I can load all my swfs at the beginning and type cast them in
other swfs later. Fantastic!

Thanks again!
Reply all
Reply to author
Forward
0 new messages