1

So I have 'home.swf' and 'play.swf' when I run apps I open home.swf first.

on home.swf there's button to play..

//home.swf
btnPlay.addEventListener(MouseEvent.CLICK, pickPlay);
function pickPlay(event:MouseEvent):void
{
    var loader:Loader = new Loader();
    var SWFRequest:URLRequest = new URLRequest("play.swf");
    loader.load(SWFRequest);
    addChild(loader);
}

on play.swf there's a button back to home

//play.swf
btnHome.addEventListener(MouseEvent.CLICK, clickHome);
function clickHome(event:MouseEvent):void
{
    var loader:Loader = new Loader();
    var SWFRequest:URLRequest = new URLRequest("home.swf");
    loader.load(SWFRequest);
    addChild(loader);
}

the problem is.. when i run it so many times.. like play and back to home, and play again.. the apps will lag. because it's like I just load home and play again and again but not unload it...

now I need to unload it but someone tell me to make main.swf to load and unload.. so when first run main.swf call home.swf first... and when we click play.. main.swf will unload home.swf and load play.swf

any solution? I hope you can get what I mean :)

GandhyOnly
  • 325
  • 2
  • 5
  • 18
  • i already edit my question and I hope you can undestand it @HITMAN – GandhyOnly Jun 06 '15 at 08:45
  • Why does play.swf load itself, if your intention is to go "back home"? – null Jun 06 '15 at 11:47
  • @null then what should i do? – GandhyOnly Jun 06 '15 at 15:42
  • @GandhyOnly could you please first explain what your intention is behind doing it? I don't understand it at all. If you want to go back to home, I'd understand if you load home.swf, but why play.swf? How does this even get you back to home? – null Jun 06 '15 at 15:46
  • @null there's wrong on my question.. i already edit it..play.swf for play a game... and there's a button to back home.. and can pick another game... my intention is to unload swf before load new SWF.. story above just an example to unload before load new SWF – GandhyOnly Jun 06 '15 at 15:54

1 Answers1

0

Your Loader is referenced by a local variable in the function. This makes it hard to reference it later.

Change your code to something like this:

btnPlay.addEventListener(MouseEvent.CLICK, pickPlay);
var loader:Loader = new Loader();
addChild(loader);

function pickPlay(event:MouseEvent):void
{
    var SWFRequest:URLRequest = new URLRequest("play.swf");
    loader.load(SWFRequest);
}

The simplest way of doing that would be to place the "back home" button in home. If this is possible from the visuals, consider this over placing the button into play.swf

If you do want to put the button into play.swf, your best option would be to dispatch an event in play.swf, when the button is pushed. You listen for the Event in home.swf and unload the file if is occurs.

However you trigger it, the loader provides two methods to unload the content: unload() and unloadAndStop() You want to call either one to get rid of the loaded file.

null
  • 5,207
  • 1
  • 19
  • 35