Skip navigation
Currently Being Moderated

Unload and Stop swf from stage

Jun 7, 2012 7:31 AM

Tags: #button #3 #actionscript #home #unloadandstop #as

Hi,

 

I'm a newbie to Flash and Actionscript. I need to give links to around 20 swf files via buttons from the menu.swf. I'm able to give links. But when I come back by clicking HOME button kept at the newly loaded swf the audio files are still being played at the background.

 

Yes, It has got audio files playing at the back for each slide in the new swfs. When Click on the same link again in menu.swf again the files are being played with the unfinished old file. It's really hectic.

 

I need to unload it completely from stage when I come back to menu.swf by clicking on HOME button from any of 20 categories.

 

I used unloadAndStop but it is not doing. I hope that I'm commiting mistakes at somewhere but couldn't predict.

 

Need some help !

 

FOR HOME BUTTON:


var _content:Loader = new Loader();

 

function loadContent(content:String):void {

    if (_content != null) {

        _content.unloadAndStop(true);

        removeChild(_content);

        _content = null;

    }

    var loader:Loader = new Loader();

    loader.load(new URLRequest(content)); //---->>>> I don't really know what should be put in this :-(

    _content = addChild(loader) as Loader;

}

 

Home_Button.addEventListener(MouseEvent.CLICK, home);

 

function home(event:MouseEvent) {

    _content.unloadAndStop(true);

 

    var homeSWF:URLRequest = new URLRequest("demo.swf");

    _content.load(homeSWF);

    addChild(_content);

}

 

FOR MENU.swf button

 

var loader:Loader = new Loader();

 

Category1_Button.addEventListener(MouseEvent.CLICK, category1);

 

function category1(event:MouseEvent):void {

    var Category1SWF:URLRequest = new URLRequest("Category1.swf");

    loader.load(Category1SWF);

    addChild(loader);

 

}

 
Replies
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 7, 2012 7:37 AM   in reply to kumar0607

    there are some problems with that code.  but to start fixing that, how many different swfs do you need to load at any one time?

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 8, 2012 12:30 PM   in reply to kumar0607

    if you only need to load one swf at any one time, use one loader:

     

    var _content:Loader = new Loader();

    addChild(_content);

     

    function loadContent(content:String):void {

        if (_content.content != null) {

            _content.unloadAndStop(true);

        }

     

        _content.load(new URLRequest(content));

    }

     

    Home_Button.addEventListener(MouseEvent.CLICK, home);

     

    function home(event:MouseEvent) {

     

        var homeSWF:URLRequest = new URLRequest("demo.swf");

    if(_content.content){

    _content.unloadAndStop();

    }

        _content.load(homeSWF);

    }

     

    FOR MENU.swf button

     

     

    Category1_Button.addEventListener(MouseEvent.CLICK, category1);

     

    function category1(event:MouseEvent):void {

        var Category1SWF:URLRequest = new URLRequest("Category1.swf");

    if(_content.content){

    _content.unloadAndStop();

    }

        _content.load(Category1SWF);

     

     

    }

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 8, 2012 1:05 PM   in reply to kumar0607

    use this code:

     

     

    Home Button:

     

    var _content:Loader = new Loader();

    addChild(_content);  // _content, a loader with no content is added to this timeline

     

    function loadContent(content:String):void {

        if (_content.content != null) {

            _content.unloadAndStop(true);

        }

     

        _content.load(new URLRequest(content));

    }

     

    Home_Button.addEventListener(MouseEvent.CLICK, home);

     

    function home(event:MouseEvent) {

     

        var homeSWF:URLRequest = new URLRequest("demo.swf");

    if(_content.content){

    _content.unloadAndStop();

    }

        _content.load(homeSWF);

    }

     

     

    For Menu.swf Button:

     

    Category1_Button.addEventListener(MouseEvent.CLICK, category1);

     

    function category1(event:MouseEvent):void {

        var Category1SWF:URLRequest = new URLRequest("Category1.swf");

    if(_content.content){

    _content.unloadAndStop();

    }

        _content.load(Category1SWF);

     

     

    }

     

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 8, 2012 1:58 PM   in reply to kumar0607

    if you are not seeing a loaded swf it is because you are adding other objects to the display list AFTER _content is added and they are covering the loaded swfs. 

     

    to remedy, re-execute

     

    addChild(_content);

     

    to move _content to the top of the display list.

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 8, 2012 4:30 PM   in reply to kumar0607

    are you publishing for fp 10 or better?

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 9, 2012 12:01 PM   in reply to kumar0607

    which is your main swf?

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 9, 2012 4:54 PM   in reply to kumar0607

    then from the main timeline in category1.swf, use:

     

     

    Category1_Button.addEventListener(MouseEvent.CLICK, category1);

     

    function category1(event:MouseEvent):void {

        var menu:URLRequest = new URLRequest("menu.swf");

    if(MovieClip(parent)._content.content){

    MovieClip(parent)._content.unloadAndStop();

    }

       MovieClip(parent)._content.load(menu);

     

     

    }

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 10, 2012 7:00 AM   in reply to kumar0607

    i think there's a terminology issue.

     

    your main swf is the swf that is embedded by your html page.  it is the first swf to be displayed and it cannot be unloaded using actionscript. 

     

    it should contain loader, the only Loader instance you need.

     

    that main swf may or may not be main.swf. 

     

    in your main swf, you should use:

    /////////////////////////////////

    var loader:Loader = new Loader();

    addChild(loader);

     

    function loadF(fileS:String):void {

        if(loader.content){

            loader.unloadAndStop();

        }

        loader.load(new URLRequest(fileS));

        addChild(loader);

    }

    function unloadF():void{

    if(loader.content){

    loader.unloadAndStop();

    }

    }

    /////////////////////////////////////////

     

    don't change anything between the ////// lines

    ----------------------------------------------------

    from your main swf, if you want to load "catgoryxyz.swf", use:

     

    loadF("categoryxyz.swf");  // this argument in this line you can and will change

     

    from you main swf, if you want to unload whatever's been loaded (and appear to return to your main swf), use:

     

    unloadF();

    ---------------------------------------------------

    from the main timeline of any loaded swf (like categoryxyz.swf), if you want to load "categoryabc.swf", use:

     

    MovieClip(this.parent.parent).loadF("categoryabc.swf");  // the argument in this line you can and will change

     

    if you want a swf to unload itself (and appear to return to your main swf), use:

     

    MovieClip(this.parent.parent).unloadF();

    -----------------------------------------------------------------

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 10, 2012 5:01 PM   in reply to kumar0607

    is "menu file" your main swf?  ie, the first one that appears when your application starts?

     
    |
    Mark as:
  • kglad
    62,145 posts
    Jul 21, 2002
    Currently Being Moderated
    Jun 11, 2012 4:40 PM   in reply to kumar0607

    one swf (the main one or menu.swf in your situation) always remains loaded.  you can stop all sounds in it (SoundMixer.stopAll() ) and you can position a stage colored and sized rectangle over everything in your main swf so it "appears" to be unloaded but you can't unload it.

     

    and no, one swf will not overload memory unless it is creating more and more objects.  you can use System.totalMemory in an enterframe loops (for example) to check whether you have a memory leak.

     

    p.s.  please mark helpful/correct responses, if there are any.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points