567 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Ireland
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
iamdok says

I’m building module loading into an OOP template and I’m having a problem with type casting.

I have 3 classes, Main, Modue & AboutModule. The AboutModule Class extends the Module Class. The About Clas consists of a Class and a linked fla with library assets. The document class of the fla is the AboutModule classpath.

The main class loads the swf file made from the AboutModule fla and attempts to cast it as type Module. This should be working seeing as the AboutModule extends the Module Class, but its giving me errors.

Here’s the swf loading complete casting operation:

                //got module
        private function moduleCompleteHandler(e : Event) : void
        {
            var loader : Loader = (e.currentTarget as LoaderInfo).loader;
            var module : Module = loader.content as Module; //returns null
                        //which means the AboutModule is not part of the Module type!

            trace("loader.content= " + loader.content); //AboutModule
            trace("module= " + module); //null

            contentHolder.addChild(module);
        }

Perhaps the confusion comes from the linking of the fla with its class. Maybe there is a better way of doing this. What is the normal way of linking library assets in swfs to OOP classes?

Any ideas?

Donagh

101 posts
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • Sold between 100 and 1 000 dollars
PremiumFlash says

What error do You get?

567 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Ireland
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
iamdok says

Because I’ve used “as Module” instead of “Module()” casting method, it’s not throwing an error, but seeing as “null” is being returned, I am then getting the error on the last line, with the “addChild” – child cannot be null.

101 posts
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 1 and 9 users
  • Sold between 100 and 1 000 dollars
PremiumFlash says

I see. I am not sure if the runtime environment can determine the loaded swf inheritance chain. You should cast to AboutModule or use Interface and cast to interface IModule.

Here is nice example of it: http://stackoverflow.com/questions/1554924/as3-loading-in-a-swf-as-a-custom-type

579 posts
  • Bought between 10 and 49 items
  • Europe
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • Sold between 1 000 and 5 000 dollars
SparkyFlash says

Donaugh, try this one http://www.flashdevelop.org/community/viewtopic.php?f=9&t=5741&start=0

Export the MovieClips in swc, extend the classes, import them to your project and use them :) Should work just fine.

In your case, I understand where you coming from willing to cast the loaded swf all as Module, but I don’t like this approach. Try the above link, maybe you’ll find something useful for yourself.

5278 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
+5 more
CodingJack says

If your AboutModule class is the document class for your AboutModule fla, doesn’t it extend Sprite or MovieClip?

111 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
  • Taiwan
savorks says

This should working fine.

Maybe Module Class in Main.fla and Module Class in AboutModule.fla is refer to different Module.as file.

Make sure loader fla and loaded fla use same ClassPath, also be careful library asset Class name.

For example, if there is a asset in Main.fla library named Module(in Class name), it will cause something you encountered.

111 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Sold between 1 000 and 5 000 dollars
  • Taiwan
savorks says
If your AboutModule class is the document class for your AboutModule fla, doesn’t it extend Sprite or MovieClip?

If Module Class extends MovieClip or Sprite, it will works. :)

567 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Ireland
  • Referred between 10 and 49 users
  • Sold between 10 000 and 50 000 dollars
iamdok says

Thanks for the comments guys.

Yes the Module class extends Sprite Class.

It seems to be working now. I think there was a conflict in class names within the library assets, and I think Flash had also forgot my custom source path for custom classes. Or something!

I’ll make a post of the method in a couple of hours, but now have to run.

Thanks for the help.

Donagh.

5278 posts The Dude Abides
  • United States
  • Exclusive Author
  • Has been a member for 5-6 years
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Bought between 100 and 499 items
  • Referred between 100 and 199 users
+5 more
CodingJack says
If your AboutModule class is the document class for your AboutModule fla, doesn’t it extend Sprite or MovieClip?
If Module Class extends MovieClip or Sprite, it will works. :)

That’s good to know :)

Donagh,. if you don’t create a class for something and export it for AS, it will inherits the root package. I’ve had the same conflict before, so I just renamed one of the clips like you did to solve it.

by
by
by
by
by
by