PDA

View Full Version : unloadMovie in AS3???



stevensunsunsun
December 5th, 2009, 08:39 AM
Hi there -help- I've just moved to AS3... and have been using:

unloadMovie("_parent.holder");

on the last frame of my externally loaded SWFs to unload them from the main stage.

But how do I do this in AS3???

I'm using the code below to load (on button click) a 20second video clip onto the main stage. At the end of the clip I want it to unload automatically, or removeChild??? but I can't find anything anywhere that shows me how exactly... it's got to be pretty simple right!?


function newvid(event:MouseEvent) {
trace("you clicked me");
var loadit = new Loader();
addChild(loadit);
loadit.load(new URLRequest("cover_movie.swf"));
}
cover_movie.addEventListener(MouseEvent.MOUSE_DOWN , newvid);

wvxvw
December 5th, 2009, 11:25 AM
Loader#unload() in FP9
Loader#unloadAndStop() in FP10

stevensunsunsun
December 5th, 2009, 12:01 PM
Loader#unload() in FP9
Loader#unloadAndStop() in FP10

Hi - thanks for the code! I'm not too sure how to implement it though.

On my main stage, I have one button with this:

function newvid(event:MouseEvent) {
trace("you clicked me");
var loadit = new Loader();
addChild(loadit);
loadit.load(new URLRequest("movie.swf"));
}
movie.addEventListener(MouseEvent.MOUSE_DOWN, newvid);

And on the external SWF, I have this code now later on in the timeline:

Loader#unloadAndStop();

What I am missing?

Thank you!

stevensunsunsun
December 5th, 2009, 12:08 PM
... I got pretty close using:

parent.removeChild(this);
SoundMixer.stopAll();

But this only removes it from screen, it is still running and loaded. When the SWF loops back to the above code the 2nd time it throws out an error because there is no object to remove.

I've read that I need to set up an Event Listener to completely remove it... but no idea how!

damonlee3
December 5th, 2009, 01:37 PM
... I got pretty close using:

parent.removeChild(this);
SoundMixer.stopAll();

But this only removes it from screen, it is still running and loaded. When the SWF loops back to the above code the 2nd time it throws out an error because there is no object to remove.

I've read that I need to set up an Event Listener to completely remove it... but no idea how!

in your movie.swf, try adding this:

this.loaderInfo.addEventListener(Event.UNLOAD, unloadHandler);

//remove event listerners from this swf
function unloadHandler(e:Event):void
{
this.unloadAndStop();
//remove the event listener
this.loaderInfo.removeEventListener(Event.UNLOAD, unloadHandler);
}

Actually, you should use the above method in any external swf that contains event listeners. For example, if you had a stage listener, you would place, say,

stage.removeEventListener(MouseEvent.MOUSE_DOWN, myFunction); within the unloadHandler function.

AND

In your parent swf, assign an unload function to whatever button you're using to load your next movie:


myBtn.addEventListenter(MouseEvent.CLICK, loadMyOtherMovie);
myBtn.addEventListener(MouseEvent.CLICK, unloadCurrentSWF);

function unloadCurrentSWF(e:MouseEvent):void
{
//trace("unloading myLoader");
//unloades SWF from memory
//**AND stopping video's audio channel**
myLoader.unloadAndStop();
}



NOTE: unloadAndStop() only available in when you export to Flash Player 10

stevensunsunsun
December 5th, 2009, 02:22 PM
Hi, thanks for the detailed explanation... but I need to see these things actually working to then try and understand what each bit is doing. Then I can start removing bits, or changing bits... the only way I know how to learn AS.

I've attached 2 very simplified versions of the files I am using, with my code and your code... can you tell me why this doesn't work?

...I finally started to understand AS2, only to now have to go through all this again!!

Thank you! ...or anyone that can take a look at the attached FLAs,

Steven