简体   繁体   中英

Loading and using SWF files

I'm new to AS3, and am trying to understand how externally loaded SWFs work in AS3. Since Flash 4/5, it was common to have one main SWF file in a Flash web project, and then load other SWF files into it, often for various "sections" of a website or web project. In the main file, we'd have masks animating the container movieclip(in which external sections/SWF files were loaded) and have animations and transitions play as the section finished loading and the loaded content was displayed.

In AS3, I've used the Loader class to load and display the external file, my main problem is in communicating with the loaded content, call it's functions, or call root functions from it.

In AS2, we could use someMovieClip.loadMovie("ExternalContent.swf") and the ExternalContent file would load inside someMovieClip. You could access functions on the "External.swf" main timeline using someMovieClip.function();. And inside the "ExternalContent.swf", we could use _root.function() to access functions in the main file ExternalContent was being loaded into. Doing this in AS3 seems bizarre and neurotic, and I feel like I'm missing something fairly basic here.

//Loading in ExternalContent.swf into the sprite
//ExternalContent has a movieclip called "boxes" on it's main timeline
//boxes has a boxesPrompt() function in it's timeline.

var sprite:Sprite = new Sprite();
addChild(sprite);

var loader:Loader = new Loader();
loader.load(new URLRequest("ExternalContent.swf"));

loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);

function onLoaded(event:Event):void
{
    sprite.addChild(event.target.content);

        sprite.boxes.boxesPrompt();
        //Flash gives the following compiler error at the above
        //Scene 1, Layer 'Layer 1', Frame 1, Line 21 1119: Access of possibly undefined property boxes through a reference with static type flash.display:Sprite.

        //But when I comment out sprite.boxes.boxesPrompt() and use this, it works:
        event.target.content.boxes.boxesPrompt()
}

The boxesPrompt() function inside the "ExternalContent.swf" just traces it's parent, grand-parent, and great grand-parent - trace(this.parent.parent.parent);. And when I call that function inside the onLoaded event-handler using "event.target.content.boxes.boxesPrompt()", it shows that the Boxes object(which was on the main timeline of External.SWF), has a parent movieclip, a grand-parent sprite, and a great grand-parent object mainTimeline.

I thought re-parenting the loaded content into the sprite would allow me to access the loaded content as easily as loadMovie() used to be - accessing loaded content like it was present directly inside the clip it was loaded in. But that doesn't work at all.

So to rephrase, my question is:

  • How do I communicate from the main "loader" SWF file, with the content that's loaded in. I don't want to communicate using event.target.content.{etc} because then I'd only be able to address the loaded content inside the Loader's event.complete event handler.

  • How do I "re-parent" loaded content, so I can place it inside some movieclip/sprite on the main timeline of the loader file, rather than using some really long convoluted way.

  • How to communicate from inside the loaded content, to the main/loader file. Previously, we'd use _root.functionName() to do stuff like play some animation transitioning from the current externally loaded "section" to another section. How'd I go about doing that.

AS2 & AS3 is vastly different. But you will have to swallow the fact that AS3 has been developed as an improvement over AS2. So any transition you make, is also for the better.

For eg : The _root in AS2 allowed global objects & variables to accessed & changed anywhere, which is a bad practice & leads to non maintainable code in a project.

Having said that, let me address your questions:

  • If you are able to get access to the loaded content with event.target.content ... you should save it inside a,say class variable & may access it later elsewhere in the class.

    You must understand that you will be able to access the content only after loading it, so have to wait for it to complete anyway & event.complete handler is probably your best bet.

  • I doubt if you can pick random content from a loaded swf & re-parent it into the current swf.As explained you might not have a long convoluted way.

  • Accessing the parent could be done in many ways. You can use .parent or actually call a function from the parent swf passing its reference to the child.


var sprite;
addChild(sprite);

var loader:Loader = new Loader();
loader.load(new URLRequest("ExternalContent.swf"));

loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);

function onLoaded(event:Event):void
{
        sprite = event.target.content;

        //This should work
        sprite.boxes.boxesPrompt();
}

See this example for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM