Flash crash (ends up in a restart loop) when loading an external swf
Im working with FlashDevelop and have two main projects, all pure AS3 projects.
When trying to load my second project from my main project I get all kinds of errors.
The Main class of the main project extends Sprite and the Main class in the "to-be-imported" project extends MovieClip. Looking at the loading of the swf in the debug window in FD it all seems fine:
[SWF] 'pathToSwf'\secondProject.swf - 410 626 bytes after decompression.
If i try to assign the loaded swf to a newly created MovieClip I get a coercion failiure:
swfContent = loader.content; // =>
Type Coercion failed: cannot convert Main@46c0201 to flash.display.MovieClip.
So, typecasting the loaded content like so:
swfContent = loader.content as MovieClip;
removes that error but then I fall into the next pit as I try to call addChild:
Error #2007: Parameter child must be non-null.
Trying to get around the issue I tried to add the loader directly into the container where I want to show the external swf. This is when the real interesting problems begin:
targetContainer.addChild(loader);
My main application now hang, restarting in a never ending lo开发者_运维技巧op. I have no idea why..
So my issue is really. How can my external swf be loaded but then again be null. It works perfectly fine when I run the external swf by itself...
Use getQualifiedClassName and getQualifiedSuperclassName functions (and even describeType if you must) on loader.content
to get its exact type information.
loader.content as MovieClip
returns null
because loader.content
is not a MovieClip
- casting with as
keyword silently returns null
when it fails. Is there any chance that the loaded content is an AS2 movie clip instead of AS3 movie clip? In that case getQualifiedClassName
will return "AVM1Movie"
.
The latter issues is weird, but first try changing the type of swfContent to Sprite
. A main class does not always extend MovieClip
, and judging from the error message it indeed doesn't in this case.
Your swfContent
will be null, if it cannot be casted to MovieClip
. That is how the as
operator is supposed to work when type coercion fails.
Modify your assignment operation like this:
var swfContent :MovieClip = MovieClip(loader.content);
You might want to encompass the assignment in a try...catch
block, as an error will be thrown in case of failure, instead of swfContent
being set null, as with as
.
So the problem was that the main class of my loaded swf had the same name as the swf I was loading from. This led to that when flash tries to execute the loaded swf it actually calls the parent MAIN class which results in the looping behaviour.
To avoid this DHuntrods suggested to change the application domain which solved the issue.
loader = new Loader();
var AD:ApplicationDomain = new ApplicationDomain( null );
var context:LoaderContext = new LoaderContext( false, AD );
loader.load(new URLRequest(path), context);
精彩评论