Remove SWF file after loading a Different SWF file using "Loader" in Flash (AS3) - actionscript-3

I am trying to load multiple swf files. But the problem is that whenever I load them (back and forth; vice-versa), my file keeps on slowing down. I looked a solution from the internet, such as using "unload" and "removeChild" but I can't get a clear solution and I don't know where to put it. Any help is very much appreciated..Here's my code on how I load my swfs:
MainMenu.swf:
//***************CLICK GALLERY*********************//
Gallery_btn.addEventListener(MouseEvent.CLICK, Gallery);
function Gallery(event:MouseEvent):void{
var ldr1:Loader=new Loader();
ldr1.load(new URLRequest("Gallery.swf"));
addChild(ldr1);
}
Gallery.swf:
//***************CLICK QUIT******************//
Quit_btn.addEventListener(MouseEvent.CLICK, Quit);
function Quit(event:MouseEvent):void{
var ldr1:Loader=new Loader();
ldr1.load(new URLRequest("MainMenu.swf"));
addChild(ldr1);
}

You need to use removeChild and remove every listener. You can unload swf by calling unload on your loader or unloadAndStop.
You can read this article for more information.
UnloadAndStop from Adobe
Unload SWF

Related

How to play two swf files in as3

hai i want play two swf files in my action script project.In this two files one swf file works on the detection face in front of the system.Other swf plays the flv file.when face is detected player must be stooped if not player must be plays the flv file.
I know how to load the swf file but i cant handle the functionality regarding starting and stoping player.
the snippet of code shows how can i load external swf file .and i will explain each line of code in comments
public function videos(view:Sprite)
{
this.box = view;//IT GETS Sprite object from other class because of need to display the player also.
request = new URLRequest("Untitled-1.swf");
currentSWF=new MovieClip();
loader= new Loader();
loader.load(request);
box.addChild(loader);
currentSWF = MovieClip(loader.content);
loader.addEventListener(Event.COMPLETE,loadComplete);
//addChild(loader);
currentSWF.gotoAndPlay(1);//when i put this line of code in comments it plays the external swf also.
}
I hope u understand my doubt .can any one explain how to handle my things .i am new to this action script.please help me
Loaded files automatically play, unless you tell them explicitely not to. You’ll have to listen to the Event.INIT event, and stop the movie there:
loader.AddEventListener(Event.INIT, initLoader);
function initLoader (event:Event)
{
MovieClip(event.currentTarget.content).stop();
}
This will stop the movie before it is attached to the stage, and before it starts playing—so it won’t do that unless you start it again.
Note that you shouldn’t access loader.content in any way before the INIT or COMPLETE events, as it’s very likely that the content isn’t loaded then. As such you should put all your manipulating actions into the COMPLETE event:
box.addChild(loader);
loader.addEventListener(Event.COMPLETE, loadComplete);
function loadComplete (event:Event)
{
// Now it’s safe to access the `content` member:
currentSWF = MovieClip(loader.content);
// Of course this one would play the movie again, so you probably want
// to call that later on a button click or something.
currentSWF.gotoAndPlay(1);
}

as3 loading screen

How do you create these "loading, please wait" splash screens with a loading bar and percentage for your swf? Does as3 have some built-in methods or should it be designed from scratch?
I mean, how do you detect that your swf file is being loaded into client's browser?
Oh and I have all my content stored in one frame only. pure as3 code.
If all of your code is on the timeline, then the easiest way is to make a second swf that is only used for loading your main swf. If your main swf is called main.swf, the code in your loading swf would be something like this:
//make a loader
var loader:Loader = new Loader()
//listen for loading progress
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
//listen for when the load is finished
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
//load begins...
loader.load(new URLRequest("main.swf"));
function onProgress(event:ProgressEvent):void
{
//calculate how much has been loaded
var percentageLoader:Number = event.bytesLoaded / e.bytesTotal;
//use your percentage number here to drive a loader bar graphic
}
function onComplete(event:Event):void
{
//remove listeners now that loading is done
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
//add loaded swf to the stage
addChild(loader.content);
}
As an aside, externalizing your assets opens up new preloading possibilities. Instead of having everything in your main swf, you can have your main swf load external assets, and preload those assets. Since your main swf would then be small, there would be no need to have a second swf just for loading your main swf.
There's a few different ways to do this.
Either you can have a two step process - create a swf file that simply loads in the main file, reporting progress (by listening to the progress-event on the loader) and then adding the loaded swf as a child.
Or, you can place all your content on frame 2, and stay on frame 1, checking the root.loaderInfo.bytesLoaded compared to root.loaderInfo.totalBytes values, reporting the percentage and then moving on to frame 2 when done.
Personally, I think method 1 is better, since you don't need to sully yourself with frames.
There is also a third way which uses some weird meta-data tags to simulate the second way, but without frames. It's a bit messy, but should be googleable.
Flash has its own component you can use to accomplish this:
http://help.adobe.com/en_US/ActionScript/3.0_UsingComponentsAS3/WS5b3ccc516d4fbf351e63e3d118a9c65b32-7fa4.html
Or you can build your own progress bar from scratch:
http://www.zedia.net/2008/the-right-way-to-do-a-preloader-in-as3/
There are lots of resources on the web that allow you to customize the progress bar- browse around and find one that works for you and your coding style.

Loading SWF in AS3 but flash keeps repeating constructor function, what should I do?

I am importing several external files using the Loader-class, and one of them is an swf-file. When doing so (I had done it successfully before, so did not expect any issues), I ran into all sorts of errors, and finally Flash crashed.
I put down a trace in the constructor function, and it didn't trace just once, but kept on tracing, suggesting that the constructor was stuck on loop. I guess the loading of too many of the same swf is what causes flash to eventually crash.
Here is my code (the swf im loading is now a simple test-file which contains an image and no code):
private var slides:Loader = new Loader();
public function DocumentClass()
{
trace(1)
slides.load(new URLRequest("Resources/Slides.swf"));
slides.contentLoaderInfo.addEventListener(Event.COMPLETE, SlidesComplete);
}
public function SlidesComplete(evt:Event):void
{
slides.contentLoaderInfo.removeEventListener(Event.COMPLETE, SlidesComplete);
addChild(slides);
}
This traces "11111111111..." and everything dies in the end.
HELP!
Try putting a stop() action at the top of the swf you load in (either in actionscript, or on the timeline). It's possible that the swf is being loaded in and is running a play and running on loop in the mean time (hence your code running over and over).
I would do a progress watch until the swf is fully loaded, then jump to your display frame:
Create a section for loading (your choice if you want to use a preloader)
Create another section (set of keyframes) for loaded content. I use keyframes because it's easy, but you could also wait to instantiate classes until loading is complete.
Below is a snippet I occasional build from:
// stop the playhead from moving ahead
stop(); // you can also use gotoAndStop("loading"); if you want
function loaderProgressHandler(event:Event):void {
// switch the framehead to main which will show your content
if(event.bytesLoaded >= event.bytesTotal) {
event.target.removeEventListener(Event.PROGRESS, this.loaderProgressHandler);
this.gotoAndStop("main");
}
}
this.loaderInfo.addEventListener(Event.PROGRESS, this.loaderProgressHandler);
Hope that helps!
I was just stuck on this same problem.
In my case it turned out that the problem was down to the swf having the same document class name as the swf that was loading it.
eg. Main.as was loading another swf that also had its document class called Main.as - Changing this to anything else solved the infinite loop.

AS3 ScrollPane dispatch event to content

i've loaded external swf into ScrollPane and i need to dispatch click event to this external swf. is it possible? ScrollPane.content.dispatchEvent(new MouseEvent(MouseEvent.CLICK,true)); doesn't work. this is obvious cuz ScrollPane.content is an DisplayObject and it have not CLICK event...
I can't use MovieClip as container for external swf cuz external swf is a documents converted to swfs using openoffice and it doesn't want to load inside MovieClip but perfectly loads inside ScrollPane and react on mouse clicks,but i need to simulate mouse click on it.
so you're saying that the following won't work or you haven't tried it?
var exSWF:MovieClip = MovieClip( ScrollPane.content );
or
var exSWF:Sprite = Sprite( ScrollPane.content );
Not sure to understand what you mean when you say that your external SWF won't load into a MovieClip.
Do you know what version of Actionscript was used for the external SWF, you can check that in debug mode by looking at the properties of the ScrollPane.content?

AS3: add event listener to loaded swf

I'm trying to listen for a custom event from an SWF I've loaded and I'm just not able to capture it. The loading code right now is just:
public function loadGame(gameSrc:String,gX:Number,gY:Number):void {
var loader = new Loader();
var addedDefinitions:LoaderContext = new LoaderContext();
addedDefinitions.applicationDomain = new ApplicationDomain();
loader.load(new URLRequest(gameSrc));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaded);
function onLoaded(evt:Event):void {
var game:MovieClip = MovieClip(evt.target.content);
game.x = gX;
game.y = gY;
chamber.mc_gameHolder.addChild(game);
Tweener.addTween(chamber.mc_gameTitle,{alpha:1,time:.75});
game.addEventListener("showQuiz",showQuiz);
}
}
I know the event is being fired from my loaded SWF because I also have a listener in there that traces out a "hello" when it's fired.
Anyone? And apologies if this has been posted before - search didn't turn up anything specific.
I ran into the same problem. Here is what you need to do. When instantiating your LoaderContext, make sure the LoaderContext is using SecurityDomain.currentDomain. That will solve your problem.
This would work only if both SWFs are AVM2Movie (made using AS3), which I assume is the case here because otherwise casting to MovieClip would have thrown an error on run-time.
Are you sure that the event is dispatched by the document class of the loaded swf and not by one of its children? Because you are calling addEventListener on game which is the document class (root) of the loaded SWF and it won't catch events dispatched by its children. Can you show the code where you dispatch the event?
It may be possible that the event is being dispatched before the Event.COMPLETE event. Try adding a listener for the Event.INIT event. The Event.INIT event is dispatched when the Loader first has access to the loaded swf's document object.
hm, shouldn't it be :
addedDefintions.applicationDomain = ApplicationDomain.currentDomain to permit the loaded video to 'access' the parent one ?
Also for testing purposes I suggest to bubble the event up to make you haven't missed out a display object in between.
Things to consider:
The loaded clip should be from a security-enabled domain. If it's not the same domain the loading flash resides at, it should be included in the crossdomain.xml file. And loaded too.
Manually setting "allowDomain" via Security.allowDomain towards the loaded clip's domain is never bad.
The event should be bubbling, as Flash might add a layer or two of containers between the "game" var and the actual content.
Both loading and loaded clips MUST be AS3.
It is possible that the target clip is trying to load things from it's own proper location, so when you load it under a different URL, it can't find the files and fails, never reaching the event-firing phase in the first place.
Create a LoaderContext that sets the ApplicationDomain to the currentDomain, then pass it with your load() call:
loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, handleLoaded );
var loaderContext:LoaderContext = new LoaderContext(false, new ApplicationDomain( ApplicationDomain.currentDomain ) );
loader.load( new URLRequest("yoMama.swf"), loaderContext );
You can confirm that you don't have a crossdomain issue by having the loading swf trace a var or function call from the loaded swf. If you get the expected result, crossdomain.xml is not your issue.
You have to consider some things in your situation.
(1) if you are loading code from the SAME domain, the app domain is not necessary.
(2) if you are loading code form DIFFERENT domains, check the [crossdomain.xml] policy file first... second... if you dont want to waste time fixing this... use PHP and curl the file to your domain... then load that (Flash will think its loading it from the same domain).