AS3 button to load movie and replace current movie - actionscript-3

I have an attractor swf with an invisible button covering it
When you click the invisible button I would like to load a new swf to replace the attractor
The following code does work but the new swf goes on top of the attractor
Any help would be appreciated
btn.addEventListener(MouseEvent.CLICK,f);
var loader_mc : Loader = new Loader();
var urlRequest : URLRequest = new URLRequest("file-to-load.swf");
function f(e:Event){
loader_mc.load(urlRequest);
addChild(loader_mc);
}

Related

Unload as2 swf nested inside an as3 container

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

Go to specific frame after external swf has been loaded AS3

I have been looking for solutions around here but I can't seem to get it right.
Basically I am trying to load an external swf after clicking on a 'Next' button and it will automatically go to a specific frame eg. frame 8 instead of frame 1.
At first I've got an error of using of using MovieClip function in a Loader and such.
Here's my code
nextBtn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_1);
var fl_Loader1:Loader;
var fl_ToLoad1:Boolean = true;
function fl_ClickToLoadUnloadSWF_1(event:MouseEvent):void
{
if(fl_ToLoad1)
{
fl_Loader1 = new Loader();
fl_Loader1.load(new URLRequest("projectnowd.swf"));
addChild(fl_Loader1);
var fl_Loader1:MovieClip = event.target.content as MovieClip;
fl_Loader1.gotoAndStop(8);
}
else
{
fl_Loader1.unload();
removeChild(fl_Loader1);
fl_Loader1 = null;
}
fl_ToLoad1 = !fl_ToLoad1;
}
You can access content of loaded swf only after event. Complete was dispatched while loading swf file on to the stage.
Define event handler method to start from 8 th frame
function loaderCompleteHandler(evt:Event):void {
var loadedMovie:MovieClipp = evt.currentTarget.content as MovieClip;
loadedMovie.gotoAndStop(8);
}
Replace if block with below lines of code
if(fl_ToLoad1)
{
fl_Loader1 = new Loader();
fl_Loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandler);
fl_Loader1.load(new URLRequest("projectnowd.swf"));
addChild(fl_Loader1);
}
Happy coding :)

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

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.

How to convert loaded SWF to bitmapdata?

I use the loader class to load some png files to the stage. The function that is called after the file is loaded is something similar to this:
private function IconLoaded(e:Event):void
{
percentLoaded_txt.visible = false;
iconLoader = Loader(e.target.loader);
icone = Bitmap(iconLoader.content);
icone.smoothing = true;
var _icon: imgBox;
_icon = new imgBox(_MAX_WIDTH, _MAX_HEIGHT, icone,0);// new imgHelper(_bitmap);
_icon.x = _main.cena.width/2;
_icon.y = _main.cena.height/2;
addChild(_icon);
}
imgBox is a class that auto resize the bitmap and allow also to add border for example...
My code works fine, to load PNG files. But I want also to be able to load swf files.
How can I do this?
Thanks.
You will need to make a copy of the loaded SWF into a Bitmap.
function copyIt(obj:DisplayObject):Bitmap {
var bd:BitmapData = new BitmapData( obj.width, obj.height );
bd.draw(obj);
return new Bitmap(bd);
}
var bmpCopy:Bitmap = copyIt(mySWF);
addChild(bmpCopy);
You can't load a swf as a bitmap data because they are totally distinct types. What you can do is load a swf and add it as a child of a movie clip to show its content.
This link shows how to load a swf. After loading it, you just need to add this as a child of a MovieClip and add this movieclip to the stage to show its content.