Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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!!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
The way I told you to do this is the proper way to do it. Have the loaded swf dispatch an event and have the main file listen for it. That way it is independent of the main file when you want to test it alone (as in when you try to compile it). You can keep trying to avoid my suggestions if you prefer.
Copy link to clipboard
Copied
I tried it with the event dispatcher, but it gives me an error:
1061: Call to a possibly undefined method unloadAndStop through a reference with static type flash.display:Loader.
In the handler of the event:
function quitEventHandler(event:Event):void {
_loader.unloadAndStop();
}
Edit1: I think it has to do with the fact that I 'm using flash cs3, player 9 (doesn't have player 10)
Edit2: I tried it with Flash CS4, Flash Palyer 10, and gives no error.
But, also, it doesn't unload the game!!!
The quitEventHandler does get called(so the event gets triggered just fine) but the _loader.unloadAndStop() does NOTHING!!!
Copy link to clipboard
Copied
In flash CS3 (Flash Player 9):
I think the problem was that I was adding the game to Stage:
stage.addChild(movie);
I tried this: addChild(movie); and works.
I also tried this: addChild(_loader); and works too.
So now I use the addChild(_loader);
And in the quitEventHandler: _loader.unload();
It works fine.
I suppose it's ok, right?
Find more inspiration, events, and resources on the new Adobe Community
Explore Now