Load and unload the external swf file using AS3(for window, IOS and android)
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
can you tell me how
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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
}
}
}
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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();
}
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
}
}
}
Copy link to clipboard
Copied
Comment out the line: import flash.display.URLRequest;
That is the wrong statement & you already are importing the correct class below it.
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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();
}
}
}
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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();
}
}
}
Copy link to clipboard
Copied
Are you importing your classes into the main.fla?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Boss any idea how I can solve this problem
Copy link to clipboard
Copied
Nope, I can't download the file.
Copy link to clipboard
Copied
ok boss. but help me to solve this prob?

