complete unload external swf and sound - actionscript-3

I am using this code for load and unload an external swf for a button. when I click on button, swf loads and when again I click on button swf unloads, but sound still exist and just swf screen disappeares.
Please correct this code, I want swf to completetely unload.
printer.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);
var fl_Loader:Loader;
//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad:Boolean = true;
function fl_ClickToLoadUnloadSWF(event:MouseEvent):void
{
if(fl_ToLoad)
{
fl_Loader = new Loader();
addChild(fl_Loader);
fl_Loader.load(new URLRequest("quiz.swf"));
printer.x=100;
printer.y=100;
}
else
{ fl_Loader.unloadAndStop();
removeChild(fl_Loader);
fl_Loader = null;
printer.x=155;
printer.y=334;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad = !fl_ToLoad;
}

If you can access the code in loaded swf than you may add Event.UNLOAD listener. In the event handler you can do some cleaning:
stop all sounds,
stop videos,
remove event listeners (specially these attached to the Stage)
This way, the unloading should be smooth and easy.

Related

How to Load & Unload File SWF at same time

I'm an as3 newbie, i have one button as name tombol fuctioning to Loading External swf & going to Specific Frame by clicking buttons, this works fine.
import flash.events.MouseEvent;
tombol.addEventListener(MouseEvent.CLICK, tekan2);
function tekan2 (e:MouseEvent):void {
function loadSWF(swfURL){
var myLoader:Loader = new Loader();
var mySWF:URLRequest = new URLRequest(swfURL);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
myLoader.load(mySWF);
}
function onCompleteHandler(loadEvent:Event){
addChild(loadEvent.currentTarget.content);
loadEvent.currentTarget.content.gotoAndStop(swfFrame);
}
function onProgressHandler(myProgress:ProgressEvent){
var percent:Number = Math.round(myProgress.bytesLoaded/myProgress.bytesTotal*100);
trace(percent+"% loaded");
}
var swfFrame:Number=2;
loadSWF("2.swf");
}
This file save as 1.swf and loading file 2.swf go to frame 2, but file 1.swf still loading, so this script loading 2 file swfs(file 2.swf frame 2 loading with file 1.swf appears). How to file 1.swf can unloading same time when I click the button, so just file 2.swf frame appears on stage.
Generally you can cancel a Loader's load operation by using the command close(). Before cancelling you need to make sure that the Loader actually loads data, otherwise an Error will be thrown:
if (myLoader.contentLoaderInfo.bytesLoaded > 0 &&
myLoader.contentLoaderInfo.bytesLoaded < myLoader.contentLoaderInfo.bytesTotal)
{
myLoader.cancel();
}
EDIT:
You can access the loader of 1.swf by root.loaderInfo.
In your function loadSWF you can add the following code to prevent 1.swf from loading further:
if (root.loaderInfo.loader && root.loaderInfo.bytesLoaded > 0 &&
root.loaderInfo.bytesLoaded < root.loaderInfo.bytesTotal)
{
root.loaderInfo.loader.cancel();
}
However, I'm not completely sure if you really can cancel the loading process of 1.swf (I have never tried out something like this). Architecture-wise, it would make sense to have a third SWF file, which is your Main SWF, and loads your other SWF files (1.swf and 2.swf, ...). In your main SWF you would need to define the Loader as class variable and not as a local variable as you are currently doing. Whenever you load a SWF, check if another loading operation is in progress and cancel it in that case. Ideally, you would place the button tombol in the Main SWF.

AS3 Preloader doesn't work locally, loaderInfo ProgressEvent.PROGRESS event does not fire

Making a custom AS3 preloader, I noticed that when my SWF is executed locally (file:///) the preloader gets stuck in the loading screen when previewed in a web browser like Chrome.
When executed from a remote server or through the standalone Flash Player, then it works. I noticed other SWF that have preloaders do not have this issue. What I need to change?
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, preloaderProgress);
function preloaderProgress(event:ProgressEvent):void {
var loadedPercent:Number = event.bytesLoaded/event.bytesTotal*100;
if (loadedPercent == 100){
this.gotoAndStop(2);
}
}
Loading locally goes very fast. Not only you should check if the file is completely loaded using the Event.COMPLETE and not the ProgessEvent but you should also make sure to register your listeners before actually calling load or the file might end up completely loaded before you register your listeners.
Following the example at http://stephenscholtz.com/201110/single-movie-flash-preloading-as3
It seems that there was no ProgressEvent.COMPLETE, but there is a Event.COMPLETE instead, a bit confusing.
I changed my code to the following, (also including some tweaks for right click menu to prohibit the user from right clicking and pressing Play before movie has loaded and such)
//Initialize any variables
var loadedPercent:Number = 0;
//Remove all items from right click Flash Player menu (except Quality, and the mandatory About... & Settings...)
var cxMenu:ContextMenu = new ContextMenu();
cxMenu.hideBuiltInItems();
cxMenu.builtInItems.quality = true;
this.contextMenu = cxMenu;
/* or disable right click menu completely (Flash Player 11.2.202.228 and over) */
/* this.addEventListener(MouseEvent.RIGHT_CLICK, onMouseRightClick);
function onMouseRightClick(event:MouseEvent)
{
return false;
} */
//Disable shortcut keys and window menubar when played from desktop Standalone Flash Player app
if (Capabilities.playerType == "StandAlone" || Capabilities.playerType == "External") {
fscommand("showmenu", "false");
fscommand("trapallkeys", "true");
}
/* Preloader: begin */
//Update loading bar and percent text as SWF loads
function onProgress(e:Event) {
//Get the amount of bytes that have been loaded / bytes total to load
loadedPercent = this.loaderInfo.bytesLoaded/this.loaderInfo.bytesTotal*100;
//Update the text of _preloaderProgress movieclip
_preloaderProgress.text = int(loadedPercent)+"%";
//Update the _preloaderBar amount by scaling horizontally as it loads
_preloaderBar.scaleX = loadedPercent/100;
}
//Go to next frame after everything loads
function init(e:Event) {
gotoAndStop(2);
}
//Attach the events to the stage
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);
this.loaderInfo.addEventListener(Event.COMPLETE, init);
/* Preloader: end */
//Stop to 1st frame and wait until it is loaded
stop();
This will allow the movie to play both remotely and locally with no problems.

external swf should unload when reaches final frame

I have a swf file loaded into a main movie. When the child is finished playing, ie reaches its final frame, I would like the swf to unload. Can anyone tell me what bits I can add to this code?
//start button
start_button_aboriginal.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_3,false,0,true);
import fl.display.ProLoader;
var fl_ProLoader_3:ProLoader;
var fl_ToLoad_3:Boolean = true;
function fl_ClickToLoadUnloadSWF_3(event:MouseEvent):void
{
//ADDED APR02 START
fl_ProLoader_3.contentLoaderInfo.addEventListener(Event.INIT, childInitHandler);
fl_ProLoader_3.load(new URLRequest("myths/myth_aboriginal.swf"));
//ADDED APR02 END
if(fl_ToLoad_3)
{
fl_ProLoader_3 = new ProLoader();
fl_ProLoader_3.load(new URLRequest("myths/myth_aboriginal.swf"));
addChild(fl_ProLoader_3);
fl_ProLoader_3.x = 114;
fl_ProLoader_3.y = 41;
}
else
{
removeChild(fl_ProLoader_3);
fl_ProLoader_3.unloadAndStop();
fl_ProLoader_3 = null;
}
// Toggle whether you want to load or unload the SWF
fl_ToLoad_3 = !fl_ToLoad_3;
}
You could
1) Use the undocumented addFrameScript function defined in the MovieClip class to place a callback on the last frame of the child SWF. Useful if you don't have control over the code in your loaded SWF.
addFrameScript() has the following signature:
addFrameScript(frameNumber, callback); //frameNumber is zero-based (unlike in the Flash authoring suite, here you would enter 0 to refer to the first frame, and 1 for the second.)
In the parent SWF, add the following:
function fl_ClickToLoadUnloadSWF_3(event:MouseEvent):void {
...
fl_ProLoader_3.contentLoaderInfo.addEventListener(Event.INIT, childInitHandler);
fl_ProLoader_3.load(new URLRequest("myths/myth_aboriginal.swf"));
...
}
function childInitHandler(event:Event):void {
MovieClip(fl_ProLoader_3.content).addFrameScript(MovieClip(fl_ProLoader_3.content).totalFrames-1, unloadChild);
}
function unloadChild() {
fl_ProLoader_3.unloadAndStop();
}
If you're worried about addFrameScript going away - don't be. When you put code on the timeline, all that code is actually compiled into functions in the document class, with frame listeners added via addFrameScript.
Remember to define function unloadChild().
function unloadChild():void {
fl_ProLoader_3.unloadAndStop();
}
-OR-
2) Dispatch an event from your loaded SWF when it reaches the final frame.
In last frame of child SWF:
this.dispatchEvent(new Event("lastFrameReached"));
In parent SWF, add the following:
fl_ProLoader_3.content.addEventListener("lastFrameReached", unloadChild);
-OR-
3) Subscribe to the ENTER_FRAME event of the child and check if the child is on its last frame.
In parent SWF, add the following:
fl_ProLoader_3.content.addEventListener(Event.ENTER_FRAME, checkIfEnded);
function checkIfEnded(event:Event):void {
if (fl_ProLoader_3.content.currentFrame == fl_ProLoader_3.content.totalFrames) {
unloadChild();
}
}
I personally prefer addFrameScript - seems to me a cleaner solution. Callback runs once, you don't have to poll and you don't need to modify the child SWF.
There are a number of ways to solve this, and it has been asked many times...
Unload child swf on last frame of child swf
When External SWF has reached Frame X, how do I unload it?
That said, one way you could resolve this without creating event listeners on the child swf, is by periodically checking the child swf for the current frame. If it's the last one on the swf, call the unload method.
// once every second, indefinitely.
var tick:Timer = new Timer(1000, 0);
tick.addEventListener(TimerEvent.TIMER, checkSWF);
tick.start();
//Load something to the stage.
var loader:Loader = new Loader();
loader.load(new URLRequest("child.swf"));
addChild(loader);
function checkSWF(e:Event):void {
if (loader.content.currentFrame == loader.content.totalFrames) {
tick.stop(); // stop the timer
loader.unloadAndStop();
}
}

Unable to load SWF in as3

I am working in flash and as3. I am new to as3. I was trying to load and unload the SWF file.
My project contains 2 files, one is index file and the other is animal file. In the index page I have button for animal. When I click on this button the animal SWF starts executing. But the problem is when I click on index button of animal SWF, it is not showing index page again, instead it is showing message as
Unable to load SWF
My index page code is:
var urlReq:URLRequest = new URLRequest("animal/animal.swf");
swfLoader.load(urlReq);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadComplete);
swfLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,
swfLoadError);
function swfLoadComplete(evt:Event):void
{
var loader:Loader = Loader(evt.target.loader);
addChild(loader.content);
swfLoader.removeEventListener(Event.COMPLETE, swfLoadComplete);
}
function swfLoadError(evt:IOErrorEvent):void
{
trace("Unable to load swf ");
swfLoader.removeEventListener(IOErrorEvent.IO_ERROR, swfLoadError);
}
So what to do to load the index SWF from animal SWF?
You probably forgot to add to the displaylist. Use addChild.
var urlReq:URLRequest = new URLRequest("animal/animal.swf");
swfLoader.load(urlReq);
this.addChild(swfLoader);
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html
Update: I see its not loaded. You have an IOErrorEvent.IO_ERROR. Mostly this means the file is not located at that url. Check the location or use a http debugger to find out which location you are trying to open.

Making a button that loads into a loaded swf file and unloads the swf on click

I am doing a portfolio type project and am having trouble creating a button that appears in my loaded swf files. I am very new to flash. The portfolio has 2 projects that get loaded when they are clicked. I need to also have a button that appears when they are click as well.
Right now I have it set to unload on click again but that doesn't work because one of the projects is interactive. My code so far is
var myLoader:Loader=new Loader ();
project2.addEventListener(MouseEvent.CLICK, project2content);
function project2content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("Project2.swf");
myLoader.load(myURL);
addChild(myLoader);
}
project3.addEventListener(MouseEvent.CLICK, project3content);
function project3content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("Project3.swf");
myLoader.load(myURL);
addChild(myLoader);
}
myLoader.addEventListener(MouseEvent.CLICK, unloadcontent);
function unloadcontent(myevent:MouseEvent):void {
removeChild(myLoader);
project2.gotoAndPlay(1);
project3.gotoAndPlay(1);
}
If I understand you correctly, you want to be able to add a close button to one or both of your project SWFs and have your parent movie pick up clicks on those buttons.
If so, try creating a button in your project SWFs and including the following code:
closeButton.addEventListener(MouseEvent.CLICK, closeHandler);
function closeHandler(event:MouseEvent):void
{
dispatchEvent(new Event("close"));
}
And replace the code which is related to the click event on myLoader with the following:
myLoader.content.addEventListener("close", closeHandler);
function closeHandler(event:MouseEvent):void {
removeChild(myLoader);
project2.gotoAndPlay(1);
project3.gotoAndPlay(1);
}