Importing images/swf into Flash

I’m working on a big Adobe Flash project, or at least much bigger in scope than what I usually do. As such I’m getting into the habit of modularising my work. I’ve been working with classes for a while but I need to go further. I began imorting images and swf files from external sources to use in a main swf as animation and static UI elements. Importing images is easy, but swf files caused me problems. How do you refer to the timeline of a swf imported into Flash when Flash doesn’t know that the imported file is a swf?

As it turns out I’d been trying to use the gotoAndPlay() method on the loader, when what I should have been doing was using the content of the loader. After a short and fruitful discussion on the subject at Actionscript.org I understood how it all works. You can see the discussion here, and here’s my eventual code:

// Original code:
package {
import flash.display.MovieClip;
import flash.events.Event;
// import classes for image/swf importing
import flash.display.Loader;
import flash.net.URLRequest;

public class gameScreen extends MovieClip {

// UI elements
public var myPlayer:Loader = new Loader();

public function gameScreen() {
myPlayer.load(new URLRequest("swf/player.swf"));
myPlayer.contentLoaderInfo.addEventListener(Event.COMPLETE, myPlayerLoaded, false, 0, true);
addChild(myPlayer);
		}

public function myPlayerLoaded (e:Event):void{
myPlayer.gotoAndPlay(10);
}
}
}
// Revised code:
package {
import flash.display.MovieClip;
import flash.events.Event;
// import classes for image/swf importing
import flash.display.Loader;
import flash.net.URLRequest;

public class gameScreen extends MovieClip {

// UI elements
public var myPlayer:Loader = new Loader();

public function gameScreen() {
myPlayer.load(new URLRequest("swf/player.swf"));
myPlayer.contentLoaderInfo.addEventListener(Event.COMPLETE, myPlayerLoaded, false, 0, true);
addChild(myPlayer);
}

public function myPlayerLoaded (e:Event):void{
trace(e.target.content is MovieClip);
e.target.content.gotoAndPlay(10);
}
}
}

I can now create all the animation for game elements individually and import them as needed. This has two major benefits. Firstly it modularises the whole visual side of the project and makes changing animation and graphics a breeze. Secondly, and more importantly, it breaks the swf file into blocks that can be loaded as required, making the eventual root swf file size much smaller and giving much more manageable load times.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *