closing a loaded SWF file without closing the whole application - actionscript-3

I'm loading an SWF file into my main app (a flash desktop app), and i want to be able to close it using an exit button without closing the whole app.
i've tried
import flash.system.fscommand;
exitButton.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
fscommand("quit");
}
i'm putting this code in the loaded SWF file, instead of closing it alone, it closes the whole window.
Any idea how can i close the loaded SWF while remaining in my main app?

Related

Cannot free memory of or unload flash swf

I'm using Adobe Animate CC with AS3. I'm having the following problem with my project (which has a lot of animation in it and hence is quite heavy)
There are 4 swf files which are in the same folder. The first one is like a blank container which loads the second swf automatically upon run. The second one has got a few buttons in it along with some animation. When the animation gets over, there is a button that loads the third swf. Now this 3rd swf has got a next button to load the next swf. The 4th swf has got back button to load the previous swf. The 3rd swf has in turn a home button that loads the 2nd swf.
Now, the problem is that when the next button is pressed, say on 2nd swf, not only should the 3rd swf open but the 2nd swf should get unloaded and the memory occupied by it should be freed. And same with the 3rd and the 4th swf.
However, the unloaded swf should open again when the back button is clicked.
If anyone could provide a link to or code for back button and load-unload, that'd be greatly appreciated.
I think it is much easier to load them all at once and then switch their visibility/attachment to the stage. However, if they are indeed too heavy to reside in the memory at once, you need a little trick to load them into a separate ApplicationDomain so they could be removed without anything left behind.
import flash.display.Loader;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
var aLoader:Loader = new Loader;
// Loading an SWF so it can be fully unloaded.
aLoader.load(new URLRequest("external.swf"), new LoaderContext(false, new ApplicationDomain));
// Unloading the loaded SWF.
aLoader.unloadAndStop(true);

ExternalInterface.call crashes flash for Chrome

There is a swf file on my website, when i click a button on it. It calls a js function, this function removes this swf and puts another.The problem is it crashes on Chrome.
I am confused about the problem. These are the cases:
-If i comment out the code which removes swf file and add next swf file, no crash happens.
-If i call javascript function(which is called from swf) from my side, it works fine.
Just an idea:
Make a test to let the call from the swf return before removing the swf from the DOM. Try something like setTimeout to let the call from the swf return and then let the remove-code be triggered by the timeout.
What kind of crash is it? AS3 exception or chrome crash?

Preloader does not work online

I've made a project that uses a preloader in the scene 1 and the content continues in the scene 2. The preloader have the follow code:
stop();
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, loading);
this.loaderInfo.addEventListener(Event.COMPLETE, loaded);
function loading(e:ProgressEvent):void {
text_txt.text = "Loading... "+int((this.stage.loaderInfo.bytesLoaded/this.stage.loaderInfo.bytesTotal)*100)+"%";
}
function loaded(e:Event):void {
play();
}
When i tested in the local machine it worked, but when i upload to the server online the preloader shots to 100% and don't show the progress percent.
I've tried export classes to the frame 2 and error still continues. I'm using just only one TextField on the stage.
It's most likely one of the following two issues:
The .swf is delivered by the server gzipped. This prevents the swf from starting until the whole file has been downloaded. You can see if it's being gzipped by opening the Dev tools (F12) in Chrome, go to the "network" tab and select the .swf and check for "Content-Encoding:gzip" under the "Response headers" headline. Note that you need to clear the browser cache first to make the browser download the .swf from the server, otherwise the Response headers won't be visible. Here's a relevant thread talking about gzip and swf: Preloader flash AS3 it's no longer working [GZIP issue].
You embed the .swf on the html page with wmode parameter set to "transparent". It's a known bug several years old, so I would be surprised if that is still relevant today. But if you do embed with wmode it's a quick test to remove that parameter and see if the problem is resolved.

eventListener flash

I am working with AS3.
I simply can't remember what eventListener to use if I want it to start my function up by it self (as in not a mouseevent.CLICK)
It is intended for a quick flash banner, that should just start up when ever a user enters the site where it is located.
You don't need to hook into an event for this.
When the site is loaded, the swf will load and play.
If the site is refreshed, then the swf will be reloaded (from cache) and be replayed
So any code that instanciates your app will be executed.
You can do loaderInfo.addEventListener(Event.COMPLETE, _YourFunctionToBeExecuted) if you want to do something when your swf file has been loaded completely
Or
You can do addEventListener(Event.ADDED_TO_STAGE, _YourFunctionToBeExecuted) if you want to run your code when the display object has been added to the stage

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.