| Game Editor: Making a Game Self-Contained |
| When you want to upload your game to the internet, you'll usually need it to be a single file. To do this, you need to import all the external files your game uses and store them within game.fla. It's generally best to only do this after all of your game's levels have been completed.
Making the game self-contained involves these steps: Add a preloader. Import all the external resources. Give them all linkage names. Drag them on-stage in the importer. Change the level loader.
External resources needed
| charset chipset levels music sound swf
Preloader
| There are many tutorials online about how to make preloaders, but the basic point is to make flash wait until everything has finished loading before playing. This is the basic code you'd put on frame 1:
CODE stop(); var total = this.getBytesTotal(); this.onEnterFrame = function(){ var loaded = this.getBytesLoaded(); if( loaded == total ){ this.onEnterFrame = function(){} play(); }// if: loading complete }// loop()
| It's also a good idea to display the progress. To do that, you'll usually create a fill bar, which is basically an empty box and a rectangular movieClip that scales until it fills the box. You'd calculate the scale uing a proportion formula like this: fill._xscale = loaded * 100 / total; Of course, there are much more creative ways to represent load progress. Just make sure the preloader doesn't take much memory, otherwise, the user would have to wait for the preloader to load, and that would defeat the point.
Importing resources
| Charsets and chipsets are imported like regular pictures. To import a level, open the level file in the game editor and select the menu option: File -> Copy Level Actionscript Then create a new movieClip in your game, and paste the code in the clipboard into frame 1. Music and sounds are imported normally as well, except you generally want to import WAV versions of your music to make them loop smoothly. (MP3's will pause slightly at the beginning) SWF files need to be copied as movieClips. You do this by opening the FLA files for each SWF file your game uses, and copying their contents over. (Keep your libraries organized)
Linkage names
| The linkage names of all your imported resoures must exactly match their relative paths & filenames. For example, if you had a song named titleScreen.mp3, the linkage for it's imported version should be: music/titleScreen.mp3 This applies even if your imported version is a WAV file, it still must end with .mp3. The map, sprite, sound, and music systems automatically look for internal versions of files when external versions are not found. So you want the linkage names to exactly match the original filenames being given to these systems. Another thing you must do in all the linkage options for these resources is un-check: "Export in first frame" This is critical! If you forget to do this, the resources will try to load before the preloader instead of after it! Which would defeat the whole point of having a preloader.
The "importer" movieClip
| When you tell flash to not load things on the first frame, their linkages won't work until flash enounters them on-stage first. So here's what you do: Create a new movieClip. Place stop() on its first frame. Then after the 1st frame, drag EVERY imported resource on-stage. For sounds and music, create multiple frames and select a sound for each one. (Use the properties panel) Finally, insert a frame in your main timeline between your preloader and your game's setup code, and drag this "importer" movieClip on-stage on that frame. When flash plays past this frame it'll encounter everything "on-stage" for a brief moment, which activates all their linkages. And since the resources are not on the importer's 1st frame, the person playing the game won't see them.
Level loader
| The last thing you need to do is alter your level-loading code so that it loads the level from the internal data if an external version of the level file cannot be found. It would end up looking something like this:
CODE #include "functions/readXml.as" loadLevel = function( file ){ // Try loading an external level file first var level_xml = new XML(); level_xml.file = file; // store the filename for internal loading level_xml.onLoad = function( success ){ if(success) {// if: external file loaded var loadedData = {}; readXml( level_xml.firstChild, loadedData ); buildLevel( loadedData.level ); }// if: external file loaded else {// if: external file failed // try placing an internal version of the level var getLevelXml = function( internal_xml ){ ROOT.internalLevel.removeMovieClip(); var loadedData = {}; readXml( internal_xml.firstChild, loadedData ); setTimeout( buildLevel, 34, loadedData.level ); }// getLevelXml() ROOT.attachMovie( level_xml.file, "internalLevel", 5, {onLoad:getLevelXml}) }// if: external file failed }// onLoad() level_xml.load( file ); }// loadLevel()
| In the code above, notice the "success" parameter in the onLoad() function. If an XML file fails to load, that variable will be false. When you place an internal level's movieClip, you'll also want to define a function inside of that movieClip (also named onLoad() This function will recieve the level's XML data as a parameter. The next thing to do is convert the XML into flash data, using the readXml() function. (imported at the top of the example) And finally, you send the data (now in the form of regular objects and variables) to whatever function or movieClip you use to construct the level, placing its map and sprites.
Video Tutorial
| Still confused? Click the link below to see how all of this is done! The "self-contained" section covers this. Video tutorial
| Last updated: April 27, 2010 |