Im having trouble implementing a preloader on a swf that I load into a movieclip in a seperate “main” swf. I have got the preloader working to update a text field as the swf loads but as soon as it loads it throws out an error and I am unable to use the key event listeners contained within the loaded swf.
I have read about the ADDED_TO_STAGE event and have tried to use it so that I am not referencing the key listeners until the swf is loaded but it still doesn’t work.
Here is my main swf code (edited for relevance):
playbutton.addEventListener(MouseEvent.CLICK, clickPlay);
var loader = new Loader();
loader.addEventListener("UnloadMe", unloadFunction);
var req = new URLRequest("game.swf");
function clickPlay(event:MouseEvent):void {
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
loader.load(req);
mymc.addChild(loader);
}
function loop(event:ProgressEvent):void {
//percent is a textfield in mymc that contains the percent loaded info
var perc:Number = event.bytesLoaded / event.bytesTotal;
mymc.percent.text = Math.ceil(perc * 100).toString();
}
function done(event:Event):void {
mymc.removeChild(mymc.percent);
mymc.percent = null;
mymc.addChild(loader);
}
function unloadFunction(event:Event):void {
Loader(event.currentTarget).unloadAndStop();
}
The code for the loaded content (“game”) is about 3000 lines long so I won’t supply it all! I’ll give you the basics though:
The main timeline has one key frame which contains the actions:
stop();
startGame();
backbutton.addEventListener(MouseEvent.CLICK, unloadswf);
function unloadswf(event:MouseEvent) {
dispatchEvent(new Event("UnloadMe", true));
}
The document class for “game” starts like this:
public function startGame() {
//various Timers and Functions...
this.addEventListener(Event.ENTER_FRAME, gameLoop);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownFunction);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpFunction);
}
Like I said, I tried putting those stage.addEventListeners inside an ADDED_TO_STAGE listener but the same error occured. Is it something to do with the “unloadswf” function on the main timeline?