views:

78

answers:

5

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 loop. 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...

A: 

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.

Bart van Heukelom
Thanks for the answer!In this case I explicitly had my Main class extend MovieClip so I would presume that it would work that way... The weird thing is that when i trace out loader.content I get [Object Main]...Any ideas to that?But, trying what you suggested (... as Sprite) I ended up directly in the strange loop that keeps restarting my application...
Tobias
Do you have control over the SWF that is loaded? The problem may be in there.
Bart van Heukelom
+1  A: 

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".

Amarghosh
Casting it as sprite did the trick but still gets me into this strange situation where the app keep restarting for some reason. This is when i call the addChild()-method...Any ideas to that?QualifiedClassName = MainQualifiedSuperClassName = Spritethanks for the tips!
Tobias
@Tobias About the restarting issue - its hard to say without knowing anything about the loaded SWF. Do you have access to the code? Look for anything doubtful in `Event.ADDED` or `Event.ADDED_TO_STAGE` handlers; Any hint in the `Event.ENTER_FRAME` handler?
Amarghosh
It´s my own SWF I´m trying to load and that is the strangest thing. Do you know of any situation where a SWF workes perfectly normal to play by itself but not being loaded?As a test I created two small AS3 projects in FD. One that only loaded the other and one that only has two rectangles (all created in code). Still I found the same issue with the restarting bit...What are the requirements of a swf that is to be loaded?Do you have use both the ADDED and ADDED_TO_STAGE for it to behave?
Tobias
A: 

Ok, so after some testing I find that even a small application like this one generates the strange crash-restart-loop that I encountered before:

package 
{
  import flash.display.Sprite;

  public class Main extends Sprite 
  {

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point

        var s:Sprite = new Sprite();
        s.graphics.beginFill(0xFF0000);
        s.graphics.drawRect(0, 0, 490, 356);
        s.graphics.endFill();

        var sa:Sprite = new Sprite();
        sa.graphics.beginFill(0x00000);
        sa.graphics.drawRect(40, 50, 200, 300);
        sa.graphics.endFill();

        addChild(s);
        addChild(sa);
    }
  }
}

Anyone ever encountered this before?

Tobias
A: 

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.

nikc
thanks for the answer, however it does not change my main issue. That when adding my loaded swf as child, the application goes haywire... :( restaring in a never ending loop...
Tobias
I cannot reproduce the behaviour you describe, with the example you provide in your answer (http://stackoverflow.com/questions/3148429/flash-crash-ends-up-in-a-restart-loop-when-loading-an-external-swf/3148889#3148889). I did, however, find a bug there; you have forgotten to import `flash.events.Event`. - Are you getting compiler errors?- Is the flash you're loading legacy code, or is it available for you to view and/or modify?
nikc
A: 

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);
Tobias
And that, is why packages were invented ^_^
Bart van Heukelom