• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Loading swfs, and allowing them to close themselves

Community Beginner ,
Sep 18, 2009 Sep 18, 2009

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.

TOPICS
ActionScript

Views

2.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 22, 2009 Sep 22, 2009

if you're going to re-use the loader, don't remove and don't null it.

Votes

Translate

Translate
Community Expert ,
Sep 18, 2009 Sep 18, 2009

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 19, 2009 Sep 19, 2009

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 19, 2009 Sep 19, 2009

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 21, 2009 Sep 21, 2009

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 21, 2009 Sep 21, 2009

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);
}


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 21, 2009 Sep 21, 2009

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 21, 2009 Sep 21, 2009

Copy link to clipboard

Copied

Might be easier to look at files...here they are.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 21, 2009 Sep 21, 2009

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);
}


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 22, 2009 Sep 22, 2009

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2009 Sep 22, 2009

Copy link to clipboard

Copied

if you're going to re-use the loader, don't remove and don't null it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Sep 23, 2009 Sep 23, 2009

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();
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2009 Sep 23, 2009

Copy link to clipboard

Copied

LATEST

you're welcome.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines