package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* ...
* @author Digitalic
*
* Proof-of-concept for adding a third-party preloader to a MMF-created SWF
* There are a few things to sort out, but it works in principle
*
* The bits that you may need to change are commented and marked with 3 asterisks //***
*
*/
public class Main extends Sprite
{
//embed the preloader SWF and create a variable in which to hold it
//*** Change 'preloader.swf' to match the SWF for your preloader ***
[Embed(source = 'preloader.swf')]
private var Preloader:Class;
//embed the game SWF and create a variable in which to hold it
//*** Change 'game.swf' to match the SWF for your game ***
[Embed(source = 'game.swf')]
private var Game:Class;
//create the the preloader as a Sprite
private var preloader:Sprite = new Preloader();
//Create the timer
//The first parameter is milliseconds and the second paramter is how many times you want it to run
//*** In the line below, the 5000 is the number of milliseconds to wait until your game is displayed ***
private var preloaderTimer:Timer = new Timer(5000, 1);
//Main function
public function Main():void
{
//waits for the stage to be initialised and calls the init routine when ready
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
//Initialisation function; this is the main section
private function init(e:Event = null):void
{
//remove the stage event listener
removeEventListener(Event.ADDED_TO_STAGE, init);
//sets the correct width/height for the preloader
//*** change the width of the preloader as required***
preloader.width = 640;
//*** change the height of the preloader as required***
preloader.height = 480;
//add the preloader to the stage
addChild(preloader);
//create an event listener for the timer which will allow the preloader to run
//for the specified amount of time (set at 5000 milliseconds in this example).
//When the timer reaches the specified time, the showGame function will be called
preloaderTimer.addEventListener(TimerEvent.TIMER, showGame);
//start the timer (which is set to 5 seconds in this example)
preloaderTimer.start();
}
//this function is called when the timer reaches the specified time (5 secs in this example)
private function showGame(evt:TimerEvent):void
{
//remove the preloader from the stage
removeChild(preloader);
//create a sprite for the game
var game:Sprite = new Game();
//add the game sprite to the stage
addChild(game);
}
}
}