Skip to main content
Known Participant
July 30, 2009
Answered

How to access functions on my main SWF from my external SWF?

  • July 30, 2009
  • 3 replies
  • 3403 views

Hello can someone help me how to access functions on my main SWF file from a loaded SWF file?

This topic has been closed for replies.
Correct answer Muzak

Forget about using parent. Dispatch an event instead.

btn3.addEventListener(MouseEvent.CLICK, btnClickHandler);

function btnClickHandler(e:MouseEvent):void {

     dispatchEvent(new Event("categoryClick");

}

In your main swf, listen for the complete event on the Loader.contentLoaderInfo.

In the complete event handler, add a listener for the categoryClick (or whatever you chose to call it) event on the loaded swf.

// event handler triggered when external swf is loaded

function loaderCompleteHandler(event:Event) {

    (event.currentTarget.content as MovieClip).addEventListener("categoryClick", categoryClickHandler);
}

function categoryClickHandler(event:Event):void {

    trace("category button clicked in loaded swf");

}

And remember, every time you use root or parent, god kills a kitten.

3 replies

Muzak
Inspiring
July 30, 2009

A child should never access its parent.

So rather than trying to access the parent (or parent.parent) you should look into event dispatching.

The loaded swf should dispatch events to which the main swf listens.

Avoid the use of root and parent like the plague

kglad
Community Expert
Community Expert
July 30, 2009

you can reference the main timeline of your main swf (assuming your loader is added to the display list) by using:

MovieClip(this.parent.parent)

from the main timeline of the loaded swf.  so, if you wanted to reference testF() with the above assumptions:

MovieClip(this.parent.parent).testF();

juanddiazAuthor
Known Participant
July 30, 2009

Hello my script on my external SWF is:

btn3.addEventListener(MouseEvent.CLICK, getUrl);

function getUrl(e:MouseEvent):void{

MovieClip(this.parent.parent).clickCategory();

}

When I load my main SWF the external swf is loaded and when I click on btn3 I get this error:

TypeError: Error #1006: clickCategory is not a function.

at btn0_fla::MainTimeline/getUrl()

What's wrong?

Muzak
MuzakCorrect answer
Inspiring
July 30, 2009

Forget about using parent. Dispatch an event instead.

btn3.addEventListener(MouseEvent.CLICK, btnClickHandler);

function btnClickHandler(e:MouseEvent):void {

     dispatchEvent(new Event("categoryClick");

}

In your main swf, listen for the complete event on the Loader.contentLoaderInfo.

In the complete event handler, add a listener for the categoryClick (or whatever you chose to call it) event on the loaded swf.

// event handler triggered when external swf is loaded

function loaderCompleteHandler(event:Event) {

    (event.currentTarget.content as MovieClip).addEventListener("categoryClick", categoryClickHandler);
}

function categoryClickHandler(event:Event):void {

    trace("category button clicked in loaded swf");

}

And remember, every time you use root or parent, god kills a kitten.

Ned Murphy
Legend
July 30, 2009

Try using...  MovieClip(this.parent)

in the main timeline of the loaded file to target whatever element of the main swf that has the loaded swf as a child. Such as in...

MovieClip(this.parent).removeChild(this);

OR

MovieClip(this.parent).gotoAndPlay(?);

kglad
Community Expert
Community Expert
July 30, 2009

that's the loader.  one more parent.

Ned Murphy
Legend
July 30, 2009

I figure I'll leave it... though I was gonna retract it ... saves explaining your follow-up.

You know I almost always check just before submitting to see if you snuck in before I'm ready to post, and this time was no exception... you managed to squeeze into about a 5 second gap of me checking that no one else posted and then posting.