Thursday, July 02, 2009

Preloading Flash SWF's without Executing Scripts

For a project we are doing at work, I had a game engine shell which would load .SWF's into the shell for playing individual vignettes and cutscenes. Because it was going to be served via a web server, I wanted to preload these vignettes during play so that when the vignette was encountered, it would play immediately, instead of the user having to wait to download the item.

However, one of the vignettes had a custom cursor in it, which would hide the regular cursor. The end result of this was that when I preloaded that SWF, it would execute that code and hide the cursor for the movie before displaying that vignette.

The solution was to use URLLoader to load the vignettes to get them into Flash Player's cache, and then, when they were needed, using the Loader class to load them into the engine:
public class SWFPreloader {
public function SWFPreloader() {
// Preload a list of SWFs here, or call preloadSWF()
this. preloadSWF( 'intro' );

public function preloadSWF( url:String ) {
var urlr:URLRequest = new URLRequest( url );
var _loader:URLLoader = new URLLoader();
_loader.dataFormat = URLLoaderDataFormat.BINARY;
_loader.load( urlr );
Using this method, you could preloadSWF() the MovieClips you will need later, and the Flash Player will put them into the cache. Load the MovieClips normally using the Loader() class, and Flash Player will pull the SWF's from the Flash Player cache instead of downloading them again from the web server.

No comments: