Loading SWF Files into Flash


The methods used to load images into Flash can also be used to load other Flash-created SWF files. In fact, this is more powerful than loading simple images into Flash because loading other Flash content can help teams of designers and developers to work together more easily. It can allow custom interfaces to be used based on individual tastes. It can even allow objects in the parent timeline to interact with objects in the loaded timeline.

There are a couple of things to consider when loading external Flash content into another Flash movie. First is the frame rate. When Flash content is completely loaded into a new Flash movie, it takes on the root timeline's frame rate whether it's faster or slower than its own, so timing of animations may be off if the two movies are on different frame rates in their individual form.

Another thing to consider is the _root. There can only be one _root to rule them all, so to speak. If code inside the loaded Flash file refers to its _root with absolute identifiers, errors will occur within the code that make it difficult to debug. You can of course use the _lockroot property to bypass this error, but not all previous versions of the Flash player support this property.

Because the methods used in loading images are the very same for loading Flash content, we can go right into examples.

The first one will load a digital clock that you can download from the website.

1.

Create a new Flash document.

2.

Save this document as loadingSWF1.fla in the same directory as the external files.

3.

Go to the first frame of the main timeline, open the Actions panel, and place this code within it:

 //load the clock in this.loadMovie("clock.swf"); 

This code is similar to the code we originally used to load an image in except that it uses a different URL.

Test the movie, and on your screen you should see a small digital clock face similar to the one in Figure 19.7.

Figure 19.7. Use the loadMovie() method to load external Flash content into your files at runtime.


That was just a simple example of loading Flash content back into Flash. The next section will go over how to manipulate Flash content that has been loaded in.

Manipulating Loaded SWF Files

If you load in Flash content from external sources, you are going to want to know how to control the Flash elements and objects that have been brought in.

Working with loaded content is just as easy as working with content that is created on the stage manually. You simply reference the movie clip the content is residing in, and there you can control variables, manipulate objects, and work with anything in that file.

Here is an example that will load in an external SWF file with a text field in it. When we click a button, it will notify the text field.

1.

Create a new Flash document.

2.

Save this document as controlSWF1.fla in the same directory as the external files.

3.

Name the layer in the main timeline actions.

4.

Create a new layer and name it button.

5.

Choose Window, Other Panels, Common Libraries, Buttons.

6.

Choose your favorite button and drag it from the library to the bottom right of the stage in the button layer.

7.

Give the button an instance name of myButton_btn.

8.

Go into the first frame of the actions layer, and place this code within it:

 //create an empty movie clip to house the SWF file this.createEmptyMovieClip("holder_mc",1); //load the SWF file in holder_mc.loadMovie("textBox.swf"); //create the event for the button myButton_btn.onRelease = function(){      //send text to the text field      holder_mc.myText_txt.text = "Clicked"; } 

The preceding code first creates a movie clip for the SWF file to reside in. It then loads the content in. After that, it creates a callback function for the onRelease event of the myButton_btn button so that when the button is released, text will be placed into the text field.

Test the movie and you will see immediately that the SWF file has indeed been brought in because the text field is clearly visible. When you click on the button, the word "Clicked" appears in the text field as in Figure 19.8.

Figure 19.8. Accessing content on a loaded SWF file is as easy as if it were made in the host movie clip.


That example loaded a SWF file into an empty movie clip. We will continue the same example by changing the code to put the SWF file in a player level number. We will also change the text sent to the text field.

You access information on a player's level by using the _leveln identifier where n represents the level you are referring to.

1.

Continuing from the preceding example, return the first frame of the actions layer, and replace its code with the following:

 //load the SWF file in loadMovieNum("textBox.swf",1); //create the event for the button myButton_btn.onRelease = function(){      //send time elapsed to the text field      _level1.myText_txt.text = getTimer(); } 

The preceding code loads the SWF file into level 1 of the Flash player. It then creates the callback function, which we have already seen, but this time instead of sending a generic message to the text field, it sends the milliseconds that have elapsed since the movie started playing, using the getTimer() function.

Test the movie, and you will see that once again, the SWF file is loaded successfully, and every time the button is clicked, a number is seen in the text field representing the amount of milliseconds elapsed.

So far we have placed external content in movie clips and levels of the Flash player. Next we will begin to put them in text fields.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net