Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Load and unload the external swf file using AS3(for window, IOS and android)

Explorer ,
Jun 30, 2013 Jun 30, 2013

For the external swf file loading, I use this code

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

load2.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

function newvid(event:MouseEvent) {

          trace("you clicked me");

          var loadit = new Loader();

          addChild(loadit);

    var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

    loadit.load(new URLRequest("scene02.swf"), _lc);

}

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

its working great but I don’t know how to unload the loaded swf files (looking: unload one by one and unload all)

_______________________________________________________________

in AS2 we have

on (release) {

          loadMovieNum("scene2.swf", 1);

          unloadMovieNum(2);

          unloadMovieNum(3);

}

but i need in AS3

TOPICS
ActionScript
17.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 30, 2013 Jun 30, 2013

You need some way of targeting the Loader object so that you can use its unloadAndStop() method.  But because you declare the Loader inside the function, you have nothing to reference it with when you are outside the function.  So you need to have some external reference available to directly target the Loader, which could be as simple as a variable, or actually declare the Loader outside, or if there will me a number of themyou can use an array.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 30, 2013 Jun 30, 2013

can you tell me how

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jun 30, 2013 Jun 30, 2013

For the one Loader you can declare it outside the function so that it is available outside the function when you need to target it

var loadit:Loader; // loader is declared outside the function

load2.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

function newvid(event:MouseEvent) {

          trace("you clicked me");

         loadit = new Loader();  // here it is instantiated

          addChild(loadit);

          var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

          loadit.load(new URLRequest("scene02.swf"), _lc);

}

//later on somewhere in some other function....

function.... etc {

          loadit.unloadAndStop(); // Loader content is removed

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 01, 2013 Jul 01, 2013

I try this but I got these compiler errors

package  {

          import flash.display.SimpleButton;

          import flash.display.MovieClip;

          import flash.events.MouseEvent;

          import flash.display.URLRequest;

          import flash.display.Loader;

          import flash.events.Event;

 

                    public class load1 extends SimpleButton {

                              var loadit:Loader; // loader is declared outside the function

 

                              public function load1() {

                                        this.addEventListener(MouseEvent.MOUSE_DOWN, loadfile);

                              }

 

                              private function loadfile(event:MouseEvent):void {

                                        trace("you clicked me");

                                        loadit = new Loader(); // here it is instantiated

                                        addChild(loadit);

                                           var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

                                           loadit.load(new URLRequest("s02.swf"), _lc);

                              }

 

                              private function unloadfile(event:MouseEvent):void {

                                        loadit.unloadAndStop(); // Loader content is removed

                              }

 

                    }

}

1.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 01, 2013 Jul 01, 2013

You're on the right track with having the Loader instance outside of the function.

For the errors, try adding/changing the following imports;

import flash.net.URLRequest;

import flash.system.ApplicationDomain;

import flash.system.LoaderContext;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 02, 2013 Jul 02, 2013

Now I use this code and this time I got these compiler errors

package {

                import flash.display.SimpleButton;

                import flash.display.MovieClip;

                import flash.events.MouseEvent;

                //import flash.display.URLRequest;

                //import flash.display.Loader;

                import flash.events.Event;

                import flash.system.ApplicationDomain;

                import flash.net.URLRequest;

                import flash.system.LoaderContext;            

               

                                public class load1 extends SimpleButton {

                                                var loadit:Loader;              

                                                public function load1() {

                                                                this.addEventListener(MouseEvent.MOUSE_DOWN, loadfile);

                                                }

                                                private function loadfile(event:MouseEvent):void {

                                                                trace("you clicked me");

                                                                loadit = new Loader();

                                                                this.parent.addChild(loadit);

                                                                var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

                                                                loadit.load(new URLRequest("s01.swf"), _lc);

                                                }

                                                private function unloadfile(event:MouseEvent):void {

                                                                loadit.unloadAndStop();

                                                }

                                               

                                }

}

2.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 02, 2013 Jul 02, 2013

uncomment the line: import flash.display.Loader

If something appears as undefined or not found, it's usually because the class needs to be imported.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 03, 2013 Jul 03, 2013

Got this compiler errors

package  {

                import flash.display.SimpleButton;

                import flash.display.MovieClip;

                import flash.events.MouseEvent;

                //import flash.display.URLRequest;

                import flash.display.Loader;

                import flash.events.Event;

                import flash.system.ApplicationDomain;

                import flash.net.URLRequest;

                import flash.system.LoaderContext;      

               

                                public class load1 extends SimpleButton {

                                                var loadit:Loader;            

                                                public function load1() {

                                                                this.addEventListener(MouseEvent.MOUSE_DOWN, loadfile);

                                                }

                                                private function loadfile(event:MouseEvent):void {

                                                                trace("you clicked me");

                                                                loadit = new Loader();

                                                                this.parent.addChild(loadit);

                                                                var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

                                                                loadit.load(new URLRequest("s01.swf"), _lc);

                                                }

                                                private function unloadfile(event:MouseEvent):void {

                                                                loadit.unloadAndStop();

                                                }

                                }

}

3.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 03, 2013 Jul 03, 2013

Comment out the line: import flash.display.URLRequest;

That is the wrong statement & you already are importing the correct class below it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 03, 2013 Jul 03, 2013

For the addChild error, the cklass you are creating extends the SimpleButton class.  The SimpleButton class is not supported by the addChild() method.

For the LoaderContext issues, if you want to use the LoaderContext class then import it...

          import flash.system.LoaderContext;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 03, 2013 Jul 03, 2013

I try this but still got compiler error

package  {

                import flash.display.SimpleButton;

                import flash.display.MovieClip;

                import flash.events.MouseEvent;

                import flash.display.URLRequest;

                import flash.display.Loader;

                import flash.events.Event;

                import flash.system.ApplicationDomain;

                import flash.net.URLRequest;

                import flash.system.LoaderContext;

               

                                public class load1 extends SimpleButton {

                                                var loadit:Loader;            

                                                public function load1() {

                                                                this.addEventListener(MouseEvent.MOUSE_DOWN, loadfile);

                                                }

                                                private function loadfile(event:MouseEvent):void {

                                                                trace("you clicked me");

                                                                loadit = new Loader();

                                                                this.parent.addChild(loadit);

                                                                var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

                                                                loadit.load(new URLRequest("s01.swf"), _lc);

                                                }

                                                private function unloadfile(event:MouseEvent):void {

                                                                loadit.unloadAndStop();

                                                }

                                }

}

4.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 08, 2013 Jul 08, 2013

Remove the line "import flash.display.URLRequest;". Your imports need to be:

                import flash.display.SimpleButton;

                import flash.display.MovieClip;

                import flash.events.MouseEvent;

                import flash.display.Loader;

                import flash.events.Event;

                import flash.system.ApplicationDomain;

                import flash.net.URLRequest;

                import flash.system.LoaderContext;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 08, 2013 Jul 08, 2013

Facing the same compiler errors….

package  {           

    import flash.display.SimpleButton;

    import flash.display.MovieClip;

    import flash.events.MouseEvent;

    import flash.display.Loader;

    import flash.events.Event;

    import flash.system.ApplicationDomain;

    import flash.net.URLRequest;

    import flash.system.LoaderContext

               

                                public class load1 extends SimpleButton {

                                                var loadit:Loader;            

                                                public function load1() {

                                                                this.addEventListener(MouseEvent.MOUSE_DOWN, loadfile);

                                                }

                                                private function loadfile(event:MouseEvent):void {

                                                                trace("you clicked me");

                                                                loadit = new Loader();

                                                                this.parent.addChild(loadit);

                                                                var _lc:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain, null);

                                                                loadit.load(new URLRequest("s01.swf"), _lc);

                                                }

                                                private function unloadfile(event:MouseEvent):void {

                                                                loadit.unloadAndStop();

                                                }

                                }

}

05.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 17, 2013 Jul 17, 2013

Are you importing your classes into the main.fla?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jul 17, 2013 Jul 17, 2013

yes

06.jpg

you can check my project in this link

http://www.delta-adv.com/nav/Load_and_Unload.zip

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 15, 2013 Aug 15, 2013

Boss any idea how I can solve this problem

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Aug 15, 2013 Aug 15, 2013

Nope, I can't download the file.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Aug 15, 2013 Aug 15, 2013
LATEST

ok boss. but help me to solve this prob?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines