Copy link to clipboard
Copied
Hi. I have had success with loading an swf on the timeline (from a button) and having it close when clicked on. However, I'd like to have a "Close" button on the loaded swf (not just the whole swf). Right now it just closes and goes back to main timeline when clicked on. The code I have so far:
var myLoader:Loader=new Loader ();
page1_mc.addEventListener(MouseEvent.CLICK, page1content);
function page1content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("page1.swf");
myLoader.load(myURL);
addChild(myLoader);
}
myLoader.addEventListener(MouseEvent.CLICK, unloadcontent);
function unloadcontent(myevent:MouseEvent):void {
removeChild(myLoader);
page1_mc.gotoAndPlay(1);
}
Can anyone help with this? I'd like the swf to have its own timeline and interaction.
if you're going to re-use the loader, don't remove and don't null it.
Copy link to clipboard
Copied
your code's not unloading that swf. you're just removing it from the display list so you can't see it. that's not much different from changing its visible property to false.
you should stop all streams (you must not have any or you would know you have a problem), unload the loader's content, remove the loader from the display list, remove its listeners and then null the loader.
to do all that from the loaded swf you'll need a path from your loaded swf's main timeline to the main timeline of your loading swf. that path is parent.parent.
Copy link to clipboard
Copied
Thanks for the insight. So the approach of loading the swf is OK. However, I
need to do all the unloading of the swf within its timeline? That makes
sense.
But my problem is whenever something is clicked on the swf (the whole swf)
it just closes. I'd like the swf to operate as if its own mini website. And
only a close button unload the movie.
Does that make sense? Has anyone seen a tutorial out there?
Copy link to clipboard
Copied
you can unload your swf from wherever you want.
delete that loader click event listener code if you don't want to make your loaded swf disappear when it's clicked.
if you want to call a main timeline function in the loading swf from the main timeline of the loaded swf, you can use:
parent.parent.functionInLoadingSwf();
Copy link to clipboard
Copied
Thanks. I think I'm getting closer but missing something. I took out all the unload code as suggested (see main time script at bottom). However, my external movie script looks like this:
close_mc.addEventListener(MouseEvent.CLICK, closeMC);
function closeMC(myevent:MouseEvent):void {
removeChild(myLoader);
parent.parent.functionInLoadingSwf();
}
Maintime timeline AS:
var myLoader:Loader=new Loader ();
page1_mc.addEventListener(MouseEvent.CLICK, page1content);
function page1content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("page1.swf");
myLoader.load(myURL);
addChild(myLoader);
}
NOTE: close_mc is obviously the button that lives within the movie clip
Copy link to clipboard
Copied
close_mc.addEventListener(MouseEvent.CLICK, closeMC);
function closeMC(myevent:MouseEvent):void {
parent.parent.removeF();
}Maintime timeline AS:
function removeF(){
myLoader.unload();
removeChild(myLoader);
myLoader=null;
}
var myLoader:Loader=new Loader ();
page1_mc.addEventListener(MouseEvent.CLICK, page1content);
function page1content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("page1.swf");
myLoader.load(myURL);addChild(myLoader);
}
Copy link to clipboard
Copied
Thanks for the quick response. This is the error I get when publishing the external swf:
1061: Call to a possibly undefined method removeF through a reference with static type flash.display:DisplayObjectContainer.
Copy link to clipboard
Copied
Might be easier to look at files...here they are.
Copy link to clipboard
Copied
use:
kglad wrote:
close_mc.addEventListener(MouseEvent.CLICK, closeMC);
function closeMC(myevent:MouseEvent):void {
MovieClip(parent.parent).removeF();
}Maintime timeline AS:
function removeF(){
myLoader.unload();
removeChild(myLoader);
myLoader=null;
}
var myLoader:Loader=new Loader ();
page1_mc.addEventListener(MouseEvent.CLICK, page1content);
function page1content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("page1.swf");
myLoader.load(myURL);addChild(myLoader);
}
Copy link to clipboard
Copied
Ahh. Interesting. Well it works the first time. However, if you try to click and open the swf again, the following error occurs:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at 09End_fla::MainTimeline/page1content()
Thanks for taking the time to look at this! I attached once more so you could see the problem.
Copy link to clipboard
Copied
if you're going to re-use the loader, don't remove and don't null it.
Copy link to clipboard
Copied
You rock kglad. Thank you for the help! For everyone else I'll post the code:
Main FLA:
function removeF() {
removeChild(myLoader);
}
var myLoader:Loader=new Loader ();
page1_mc.addEventListener(MouseEvent.CLICK, page1content);
function page1content(myevent:MouseEvent):void {
var myURL:URLRequest=new URLRequest("page1.swf");
myLoader.load(myURL);
addChild(myLoader);
}
EXTERNAL FLA:
close_mc.addEventListener(MouseEvent.CLICK, closeMC);
function closeMC(myevent:MouseEvent):void {
MovieClip(parent.parent).removeF();
}
Copy link to clipboard
Copied
you're welcome.