Skip to main content
April 2, 2014
Answered

as3 external swf close self

  • April 2, 2014
  • 3 replies
  • 2493 views

Hi,

After looking through online forums and video tutorials...

I am still faced with errors loading an external swf into my main swf (full stage width).

In my main.swf.....i have several scenes.

On one of those scenes I have an external SWF which loads to the size of my main.swf stage.

That works fine...

BUT

I now want to close the external swf on a button (in my external swf) and go back to one of my scenes in my main.SWF

I have read many forums which talk about dispatchEvent

I have tried these methods and I am still faced with errors.

I just want a button on my external.swf that closes/unloads/removes itself.......so the user can continue in the MAIN.SWF after they have played EXTERNAL.SWF.

in actionscript 2 this was a doddle,

in as3 it is an unnecessary pain in my rectum

sorry if I have missed much out

any help would be much appreciated

jayquery

This topic has been closed for replies.
Correct answer Ned Murphy

Try changing the main file loader code to more like the following (blue is new):

var defaultSWF:URLRequest = new URLRequest("gametestG.swf");

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
loader.load(defaultSWF);

function loaderCompleteHandler(evt:Event):void {
    MovieClip(evt.target.content).addEventListener("closeChild", onChildClose);
    addChild(loader);
}

function onChildClose(event:Event):void {
    loader.unloadAndStop();
    removeChild(loader);
}

3 replies

Ned Murphy
Legend
October 31, 2016

You're welcome

Ned Murphy
Legend
October 31, 2016

You do not appear to have anything that calls the movieLoaded function.  See the very first line of blue code in my example above.

October 31, 2016

its workin.. thank u very much..

Ned Murphy
Legend
April 2, 2014

It would be appropriate to follow the direction you say you found where the swf dispatches an event when it wants to be closed.  When you load the swf you assign an event listener to it in the main file for the event that it will generate.  You need to do this after the swf has completely loaded.  Then just have the swf dispatch said event via the button to close it and the main file should hear it and process unloading the Loader that brought the swf in.

If you show the code that you tried that didn't producethe desired result it should help to get it to work.

April 2, 2014

I have this code on my main.SWF scene....

var Xpos:Number = 800;

var Ypos:Number= 600;

var swf:MovieClip;

var loader:Loader = new Loader();

var defaultSWF:URLRequest = new URLRequest("gametestG.swf");

 

loader.load(defaultSWF);

addChild(loader);

childSwif.addEventListener("closeChild", onChildClose);

function onChildClose(event:Event):void

{

    removeChild(childSwif);

    childSwif.unloadAndStop();

  

}

that loads to my stage size which is all good

on my external swf I had ....

back_btn.addEventListener(MouseEvent.CLICK,unloadSelf);

function unloadSelf (e:MouseEvent){

dispatchEvent("closeChild");

}

Thanks for the reply

jayquery

Ned Murphy
Ned MurphyCorrect answer
Legend
April 3, 2014

Try changing the main file loader code to more like the following (blue is new):

var defaultSWF:URLRequest = new URLRequest("gametestG.swf");

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
loader.load(defaultSWF);

function loaderCompleteHandler(evt:Event):void {
    MovieClip(evt.target.content).addEventListener("closeChild", onChildClose);
    addChild(loader);
}

function onChildClose(event:Event):void {
    loader.unloadAndStop();
    removeChild(loader);
}