Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Loading FlashPaper 2.0 documents into Flash

t’s important to understand is that a FlashPaper document is a SWF file; you can load it as you would any other SWF file using loadMovie(), loadMovieNum(), MovieClipLoader, and so on.

One important thing to watch for is the size of MovieClip after loading. As with all SWF files loaded in Flash using loadMovie() and related calls, the MovieClip resizes itself to the native size of the loaded SWF file, which is always 550 x 400 for FlashPaper 2.0 documents.

To set a different size (to fit into an existing layout), you would typically use code similar to this:

dest_mc.loadMovie("foo.swf");
//
// wait for loading to complete
//
dest_mc._width = 300; // resize to 300x200
dest_mc._height = 200;

While this code works for Flash documents, it won’t give you the desired result; it produces a strangely squashed result. This happens because adjusting the _width and _height fields on a MovieClip doesn’t actually adjust the size—it adjusts the scaling factor.

So, you need to tell the FlashPaper document to resize itself. You can easily do this, using the setSize() call as follows:

dest_mc.loadMovie("foo.swf");
//
// wait for loading to complete
//
dest_mc.getIFlashPaper().setSize(300, 200); // resize to 300x200

This code almost works. The problem is the wait for loading to complete comment—it requires waiting a variable number of frames, depending on various factors, including the number of frames in the SWF file being loaded and connection speed.

One way to deal with this is to use the MovieClipLoader class provided in Flash Player 7. However, FlashPaper 2.0 documents require Flash Player 6 or later, so using this class might unnecessarily limit your deployment. So, there’s an alternate function that is safe for use with Flash Player 6 or later:

function loadFlashPaper( 
path_s, // path of SWF to load
dest_mc, // MC which we should replace with the SWF
width_i, // new size of the dest MC
height_i, // new size of the dest MC
loaded_o) // optional: object to be notified that loading is complete
{
var intervalID = 0;
var loadFunc = function()
{
dest_mc._visible = false;
var fp = dest_mc.getIFlashPaper();
if (!fp)
return;
if (fp.setSize(width_i, height_i) == false)
return;
dest_mc._visible = true;
clearInterval(intervalID);
loaded_o.onLoaded(fp);
}
intervalID = setInterval(loadFunc, 100);
dest_mc.loadMovie(path_s);
}

You can use this function to load a FlashPaper document into a MovieClip and resize it appropriately when loading is complete.

Note: This function assumes that the SWF file being loaded is a FlashPaper document. This function will execute forever for other SWF files, so use it with caution.

Suppose you have an existing MovieClip named “fp_mc” that you want to replace with a FlashPaper document, and you want the loaded document to be resized to be the same size as the existing MovieClip. You could use the following code:

loadFlashPaper("MyFlashPaperDocument.swf", 
fp_mc,
fp_mc._width,
fp_mc._height,
null);

If you want to do additional work when the document is fully loaded, you could also use an optional listener object. This example scrolls to the last page of the document and changes the document magnification to 33%:

var loadNotifier:Object = new Object; 
loadNotifier.onLoaded = function(fp:Object):Void
{
fp.setCurrentZoom(33);
fp.setCurrentPage(fp.getNumberOfPages());
};
loadFlashPaper("MyFlashPaperDocument.swf",
fp_mc,
fp_mc._width,
fp_mc._height,
loadNotifier);

If you want to alter the FlashPaper document toolbar, or add a listener for various events, onLoaded is an excellent place to do so. Here’s an example that hides the Print button and adds a listener that monitors the current page and zoom:

var loadNotifier:Object = new Object;
loadNotifier.onLoaded = function(fp:Object):Void
{
fp.showUIElement("Print", false);
fp.addListener(this);
};
loadNotifier.onPageChanged = function(newPageNumber:Number):Void
{
trace("You have scrolled to page "+newPageNumber);
};
loadNotifier.onZoomChanged = function(percent:Number):Void {
trace("You have zoomed to "+percent+"%");
};
loadFlashPaper("MyFlashPaperDocument.swf",
fp_mc,
fp_mc._width,
fp_mc._height,
loadNotifier);

Comments