Loading and using SWF files

I'm new to AS3 and I'm trying to understand how externally loaded SWF files work in AS3. Beginning with Flash 4/5, it was common for a single Flash web project to use one main SWF file and then load other SWF files into it, often for different "sections" of the website or web project. In the main file, we would have masks, animate the container movie clip (in which the external sections / SWF files were loaded), and the animations and transitions play as the section finishes loading and the loaded content loads.

In AS3, I used the Loader class to load and display an external file.My main problem is communicating with the loaded content, calling its functions, or calling root functions from it.

In AS2, we could use someMovieClip.loadMovie ("ExternalContent.swf") and the ExternalContent file was loaded inside someMovieClip. You can access functions in the main "External.swf" timeline using someMovieClip.function () ;. And inside "ExternalContent.swf" we could use _root.function () to access the functions in the main file that the ExternalContent was loaded into. Doing this in AS3 seems odd and neurotic, and I feel like I'm missing something simple enough here.

//Loading in ExternalContent.swf into the sprite
//ExternalContent has a movieclip called "boxes" on it main timeline
//boxes has a boxesPrompt() function in it 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 boxPrompt () function inside "ExternalContent.swf" just keeps track of its parent, grand, and big true parent trace (this.parent.parent.parent) ;. And when I call this function inside the onLoaded event handler using "event.target.content.boxes.boxesPrompt ()" it shows that the Boxes object (which was on the main External.SWF timeline) has a parent movie clip, a great parent sprite and the big grand parent mainTimeline.

I thought that overriding the loaded content in the sprite would allow me to access the loaded content as easily as the loadMovie () used to access the loaded content, for example it was present directly inside the clip it was loaded into. doesn't work at all.

To rephrase my question:

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

  • How do I load the content with a "parent" so I can place it inside some movieclip / sprite on the main timeline of the uploader file rather than using some very long convoluted way.

    / li>
  • How to interact from loaded content, into main / loader. Earlier we used _root.functionName () to do things like play some animation, jump from the currently loaded externally loaded section to another section. How would I do it.

+3


source to share


1 answer


AS2 and AS3 are very different. But you have to internalize the fact that AS3 was designed as an improvement over AS2. Thus, any transition you make is also for the better.

For example: _root in AS2 allowed global objects and variables to be accessed and changed anywhere, which is bad practice and results in unsupported code in the project.

Having said that, let me ask your questions:

  • If you can access the loaded content using event.target.content ... you should store it inside, say, a class variable and can access it later elsewhere in the class.

    You should understand that you will only be able to access the content after it has loaded, so they will have to wait for it to complete anyway & event.complete is probably the best choice.

  • I doubt you can pick random content from the loaded swf and rename it to the current swf. As explained, you may not have a long collapsed path.

  • The parent object can be accessed in a variety of ways. You can use .parent or you can actually call the function from the parent swf by 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 details .

+2


source







All Articles