This is a trick I picked up recently when I was preparing some games (Mashin’ Meeces and Star Fall) for release on portal sites and saw that it would be a bit easier (and required by some) to figure out how to change my multi-SWF application into a single SWF app.

Mashin’ Meeces was originally broken into three SWFs: game code, art assets and sounds. I did this so that I could work on the game independently from the sound or art since those were being taken care of by other people. Through my previous experience developing games I had never run into a problem with having games broken into multiple SWFs before but those projects have all been for private sites and game portals like Newgrounds and Kongregate are a new experience for me. After talking with my colleagues, doing some research and poking at the code for a bit I finally got the solution of embedding the SWFs into my main application and then load them using loadBytes.

Here is the code I used to embed the two SWFs (art and sounds) into the main class of my ActionScript project.

[cc lang=”actionscript3″ line_numbers=”true” no_links=”true” width=”100%”]
[SWF(width=’800′, height=’600′, backgroundColor=’0xffffff’, frameRate=’30’)]
public class MashinMeeces extends Sprite
{
[Embed(source=”MashinMeecesAssets.swf”, mimeType=”application/octet-stream”)]
private var swfAssets:Class;
[Embed(source=”MashinMeecesSounds.swf”, mimeType=”application/octet-stream”)]
private var swfSounds:Class;

public function MashinMeeces()
{
// Rawr! I am the constructor…
}
}
[/cc]

For the loading I had just been using a standard Loader implementation and to convert the pre-existing code all I had to do was drop the URLRequest() and change load() to loadBytes().

[cc lang=”actionscript3″ line_numbers=”true” no_links=”true” width=”100%”]
protected function addedToStage(in_event:Event):void
{
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);

var assetLoader:Loader = new Loader();
addChild(assetLoader);
assetLoader.visible = false;
assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, assetsLoaded, false, 0, true);
assetLoader.loadBytes(new swfAssets(), loaderContext);
}
[/cc]

Comments are closed.