I’ve been told by another intern where I work, that I must package the game I’m making in an swf file and then preload it.
So I’m trying that but fail.
I have two issues:
-
I use BitmapImages but don’t know anymore what to do with them. Is I preload the swf does that mean all images the game uses are automatically are preloaded as well?
-
When I try to preload my own game, it doesn’t show up. Oh uhmm… I use flashdevelop so the preloader project has a Preloader class and a Main class. My game has a Main class as well.
Below is the code I use to preload my own game:
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.utils.getDefinitionByName;
import nl.obg.www.*;
public class Preloader extends EMovieClip {
private var progressBar:Widget;
private var preloadNames:Array;
private var preloadTextId:uint;
private var loader:Loader;
private var loader2:Loader;
public function Preloader() {
initLoader();
addProgressBar();
}
private function initLoader():void {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, startup);
loader.load(new URLRequest("Game.swf"));
}
private function progress(e:Event):void {
var perc:Number = e.target.bytesLoaded / e.target.bytesTotal;
progressBar.editText("progress", "LOADING: " + Math.ceil(perc * 100).toString() + "%");
}
private function addProgressBar():void {
var midW:uint = this.width / 2;
var midH:uint = this.height / 2;
progressBar = new Widget();
progressBar.center(midW, midH);
progressBar.addCenterText("main","Waiting to preload...", 16);
progressBar.addCenterText("progress", " ", 16);
addChild(progressBar);
}
private function startup(e:Event):void {
// hide loader
stop();
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, startup);
removeItem(progressBar);
progressBar = null;
var mainClass:Class = getDefinitionByName("Main") as Class;
var main:Main = new mainClass() as Main;
main.addChild(loader);
addChild(main);
}
}
}