Skip to main content
PanosKolettis
Known Participant
April 28, 2015
Question

Switching betwwen swf files

  • April 28, 2015
  • 2 replies
  • 432 views

I have an swf file, called preloader.

It' s used as a preloader and as a menu container.

It has three buttons(this is the menu).

When I click a button, it loads another sfw (a game):

var request:URLRequest = new URLRequest(chosenGame);

var _loader:Loader = new Loader();

_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);

_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);

_loader.load(request);

In the game, I have a button, 'Quit'.

I want, when clicked, to get me back to the preloader swf.

Would it be correct to do it the same way?

var request:URLRequest = new URLRequest(preloader);

var _loader:Loader = new Loader();

_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);

_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);

_loader.load(request);

Or the preloader swf allready exists (as it was never unloaded), so it should not be loaded again?

This topic has been closed for replies.

2 replies

PanosKolettis
Known Participant
April 28, 2015

Well finally I put in the click event of the quit button (in the game): stage.removeChild(this);

Works fine!!

Anyway, an alternative could be your way Ned, thanks for the help!!

Ned Murphy
Legend
April 28, 2015

The removeChild function does not make the child go away or stop it from running/playing, it just removes it from being seen. If you restart the game or replay that same game you end up with that older child still hanging around behind the scenes.  If you do it the way I described it removes the swf entirely and stops it from playing further.

PanosKolettis
Known Participant
April 28, 2015

Ok, I will use the loader.unloadAndStop(), but I want to do it in the main timeline (the preloader).

But, I 'm trying to call a function ('test()') found in the main timeline,

I tried:

MovieClip(this.root).test();

And:

Object(parent).test();;

but they give me an error:

ReferenceError: Error #1069: Property test not found on flash.display.Stage and there is no default value.

Ned Murphy
Legend
April 28, 2015

If the preloader was never unloaded then there is no need to load it again, especially if the code that is executing is in the preloader itself.  You should be able to just stop and unload the chosenGame.

PanosKolettis
Known Participant
April 28, 2015

The preloader loads for example the first game.

Then the preloader is not visible, I guess it 's covered by the game.

So, the quit button (which is in the game) what action should it take?

I think something like preloader.removeChild(game)??

Or preloader.Loader.unloadAndStop??

But how can I access preloader swf from game swf?

Ned Murphy
Legend
April 28, 2015

If the preloader is supposed to be the main file that controls the rest of the game as it unfolds then an event listener should be assigned to the game file as soon as it is completely loaded (in your  onLoaded fnction).  The event handler for that event listener will process the code to unloadAndStop the _loader.  In the loaded game file the Quit button will dispatch the event that the preloader is listening for to indicate it is ready to be removed.

Example...


In your loading/parent swf, listen for the complete event on the Loader.contentLoaderInfo.  In the complete event handler, add a listener for the event on the loaded swf.


// event handler triggered when external swf is loaded
function onLoaded(event:Event) {
    MovieClip(event.currentTarget.content).addEventListener("quitTriggered", eventHandler);
}

function eventHandler(event:Event):void {
    preloader._loader.unloadAndStop();
}

Add the following line to the Quit button code in the loaded game file

dispatchEvent(new Event("quitTriggered"));

(if you encounter a dispatchEvent problem, see: http://www.kirupa.com/forum/showthread.php?p=1899603#post1899603)