Unload as2 swf nested inside an as3 container - actionscript-3

What I'm trying is unload an as2's swf from and as3's swf containner. So It's running okay when loading the as2 swf from an as3 swf cointainner's button as you can see in the next code:
//AS3 cointainner "AS3Loader_TEST1":
var loader:Loader = new Loader();
loader.load(new URLRequest("AS2animation_TEST1.swf"));
addChild(loader);
// and for solving the pitfall, I tryed this:
var AVM_lc:LocalConnection = new LocalConnection();
loader.addEventListener(MouseEvent.CLICK, stopandback);
function stopandback(event:MouseEvent):void {
AVM_lc.send("AVM2toAVM1", "disappear");
}
=======================================================
//then, into the "AS2animation_TEST1.swf" it's written
var AVM_lc:LocalConnection = new LocalConnection();
AVM_lc.dissapear = function(){
animation_mc.unloadMovie();
}
AVM_lc.connect("AVM2toAVM1");
So, the last script unload a movieclip nested in AS2animation_TEST1.swf at addEventListener dispatched from a mouse click into the as3 containner
but that's not the way I'm looking for.
So, What do you figure out to unload the whole AS2animation_TEST1.swf

Related

Unloading swf file through button click

So I'd like to set this up to where you click on a button it loads a new scene and unloads the previous scene.
This is what I have so far.
staart.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event: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= 1;
loadSWF("home.swf");
}
Is it possible to unload a specific swf file or previous swf file through a newly loaded swf file?
You should create a variable for your swf.
Add this to your code :
var mc:MovieClip = new MovieClip();
// Adds the movie clip at initialization.
addChild(mc);
and modify your onCompleteHandler method like this :
function onCompleteHandler(loadEvent:Event){
// Changes the content of your movie clip.
mc = loadEvent.currentTarget.content;
mc.gotoAndStop(swfFrame);
}
shawn gave you the right answer but there are 2 other problems in your code:
There's a typo in element staart and a potential memory leak because you add event listeners without removing them. You should add the following two lines in onCompleteListener function:
loadEvent.currentTarget.removeEventListeners(Event.COMPLETE,onCompleteHandler)
loadEvent.currentTarget.removeEventListeners(ProgressEvent.PROGRESS,onProgressHandler)
If you don't remove the listeners, the garbage collector will be unable to free the memory of every loader you create on each click

How to loop SWF file loaded with Loader?

I want to loop a swf file that has been loaded with via the Loader class in AS3.
My code looks as following:
public class MyLoader extends MovieClip {
public function MyLoader() {
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("external-movie.swf");
myLoader.load(url);
myLoader.contentLoaderInfo.addEventListener("complete", function() {
});
addChild(myLoader);
}
}
From what I understand the Loader has no event for when the embedded movie is finished? Is there a way to figure that out? It must not be a AS3 implementation. I just want a movie that has been exported from Indesign to run in a loop. Thanks in advance
Especially when you have little experience programming you should avoid dirty shortcuts as they'll only get you a lot of trouble. So avoid anonymous function and avoid using string in place of static event variables.
This being said, if your loaded movie has its own timeline then it will be converted into a MovieClip. Also that movie is not embedded but loaded and that's a big difference.
Keep a reference of that movie and the loader:
private var theLoadedMovie:MovieClip;
private var myLoader:Loader;
Listen for the INIT event instead of the COMPLETE event (movies with timeline start to play when their first frame is loaded "INIT", the COMPLETE event fires when the whole movie is loaded).
myLoader = new Loader();
var url:URLRequest = new URLRequest("external-movie.swf");
myLoader.load(url);
myLoader.contentLoaderInfo.addEventListener(Event.INIT, handleInit);
In your handleInit method keep a reference of that movie:
theLoadedMovie = myLoader.content as MovieClip;
addChild(theLoadedMovie);
theLoadedMovie.addEventListener(Event.ENTERFRAME, handleEnterFrame);
in your handleEnterFrame method check the movie progress to know when it has ended:
if(theLoadedMovie.currentFrame == theLoadedMovie.totalFrames)
{
//movie has reached then end
}

Flash AS3 load and replace movie

I have a loader mc...an animation that loops - when you click a button I want a new movie to load on top and replace the loader mc
Here is my current code:
btn_load.addEventListener(MouseEvent.CLICK, btn_load_press);
function btn_load_press(e:MouseEvent)
{
var reloadRequest:URLRequest = new URLRequest("new-movie.swf");
var loader:Loader = new Loader();
loader.load(reloadRequest);
addChild(loader);
}
But this puts it on top on my loader...effectively meaning the loader is still running in the background.
This is really simple in AS2 - and I'm not sure if it's even possible in AS3
To do this we'll need to add a listener for your loader.contentLoaderInfos complete event, then remove your animation once everything has finished loading.
btn_load.addEventListener(MouseEvent.CLICK, btn_load_press);
function btn_load_press(e:MouseEvent)
{
var reloadRequest:URLRequest = new URLRequest("new-movie.swf");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loader_complete);
loader.load(reloadRequest);
addChild(loader);
}
function loader_complete(e:Event)
{
e.target.removeEventListener(Event.COMPLETE,loader_complete);
removeChild(loader_mc);
}

How to load new swf on mouseclick event of movieclip in as3?

I want to create a flip effect button, on click event of which a external swf should be opened removing all the contents of current swf.
I have created both components, a button and also a movieclip.
Neither of its load ( new URLRequest()) code working.
Below is the complete code I am using. Where btn2 is class name of my movie clip
and 2.swf is external swf I want to load.
stop();
var bc2:btn2 = new btn2();
bc2.buttonMode = true;
bc1.addEventListener(MouseEvent.CLICK, mouseClick);
var loader = new Loader();
function mouseClick(event:MouseEvent): void {
loader.unload();
loader.load(new URLRequest("2.swf"));
addChild(loader);
}
First btn2 instance what you are instantiating needs to be added for it to be visible.
Secondly you are trying to load the swf even before its loaded.
I have modified the code
stop();
var bc2:btn2 = new btn2();
bc2.buttonMode = true;
this.addChild(bc2);
bc2.addEventListener(MouseEvent.CLICK, mouseClick);
var loader = new Loader();
function mouseClick(event:MouseEvent): void {
loader.unload();
loader.load(new URLRequest("2.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler, false, 0, true);
}
function completeHandler(event:Event):void{
this.addChild(loader);
}
This should work...

Loading another swf file using AS3

I'm trying to load in another swf on a button click using Aaction Script 3.
The problem I'm having is that it just seems to load and mix the movies together. Is is possible to load and replace on stage the newly loaded swf similar to how you could do this in AS2 using loadMovieNum()
This is what I have so far:
//Add event listener for button click
backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
//Create a function for the button click
function backButtonClick(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("2.swf");
var loader:Loader = new Loader()
loader.load(request);
addChild(loader);
}
Many thanks
Use the loader like this:
//Add event listener for button click
var singleLoader:Loader = new Loader();
backButton.addEventListener(MouseEvent.CLICK, backButtonClick);
//Create a function for the button click
function backButtonClick(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("2.swf");
singleLoader.load(request);
addChild(loader);
}
What you're doing is you're creating a new Loader every single time for every new SWF you're loading. Just use a single loader and each time you load content on it, it should replace the existing content. If not, adjust the code like so:
function backButtonClick(ev:MouseEvent):void
{
var request:URLRequest = new URLRequest("2.swf");
singleLoader.unloadAndStop(true);
singleLoader.load(request);
addChild(loader);
}
See the documentation for more: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html
Update
If you want to clear everything off the stage when you do this, see the following question & answer My Actionscript 3.0 Stage will not clear.