# Preloader problem

**URL:** https://forum.kirupa.com/t/preloader-problem/294477
**Category:** Uncategorized
**Created:** [August 14, 2009, 3:32pm UTC](https://forum.kirupa.com/t/preloader-problem/294477 "2009-08-14T15:32:14Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![dst1](https://avatars.discourse-cdn.com/v4/letter/d/b2d939/32.png) [@dst1](https://forum.kirupa.com/u/dst1)
#### Post date: [August 14, 2009, 3:32pm UTC](https://forum.kirupa.com/t/preloader-problem/294477/1 "2009-08-14T15:32:14Z")

</div>

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):

```auto

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:

```auto

stop();
startGame();

backbutton.addEventListener(MouseEvent.CLICK, unloadswf);

function unloadswf(event:MouseEvent) {     
     
     dispatchEvent(new Event("UnloadMe", true));
}

```

The document class for “game” starts like this:

```auto

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?
