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

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

New Here ,
Jul 30, 2009 Jul 30, 2009

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

TOPICS
ActionScript
3.5K
Translate
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

Engaged , Jul 30, 2009 Jul 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:

...
Translate
Community Expert ,
Jul 30, 2009 Jul 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();

Translate
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
New Here ,
Jul 30, 2009 Jul 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?

Translate
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
Engaged ,
Jul 30, 2009 Jul 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.

Translate
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 ,
Jul 30, 2009 Jul 30, 2009

it's hard to understand why the op would mark your answer as correct when you're answering a question that wasn't asked and not answering the question that was asked.  but i guess that works for the op.

Translate
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
Engaged ,
Jul 30, 2009 Jul 30, 2009

Maybe because that's the proper way of doing things?

As I mentioned, accessing functions in a parent isn't the right way to go. Event dispatching is.

So to answer his question directly:

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


You don't , use Event dispatching instead.

Did I earn my points now? djeez..

Translate
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 ,
Jul 30, 2009 Jul 30, 2009

there's no point in clicking a button and no point dispatching an event and no point to defining a listener for that event.

just call whatever function when loading is complete.  ie,  use the complete event for the loader and execute that function.

if the op wanted something returned to the loaded swf from the loading swf's function, which would be the only valid reason for his question, you didn't answer it.

Translate
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
Engaged ,
Jul 30, 2009 Jul 30, 2009

Well, I guess you don't understand what event dispatching is all about then.

What he wants to do is notify whoever is listening that a "category button" is clicked, thus dispatching an event is the proper way to do so.

I'll repeat it once more, a child doesn't "talk" to a parent directly.

Does a Button component dispatch a click event or does it call a function in its parent directly?

By calling a function, you're expecting the function to actually be there, what if you want to load that same swf into another swf?

You'd have to have a function in place with the correct name. That's not how to do things properly.

You may not like the answer given, but it's the correct one

Translate
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 ,
Jul 30, 2009 Jul 30, 2009

i missed his 2nd post about clicking a button and not retrieving a return from clickCategory().

Translate
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
New Here ,
May 14, 2011 May 14, 2011
LATEST

I'd like your assistance with resolving my issue.  Ned helped me with the first part.  His explanation was thorough enough that I could follow his instructions.  However he doesn't have cs4 or 5 flash and can't access the file at the link I provided which you will find following this request.  The fla is GARAGE.  The main movieclip is garage and the nested movie is tools and inside of tools are buttons.  I left the buttons as white graphics for simplicity.  the one that sits on top of the tool chest is suppose to close the garage by playing the close animation on the garage timeline.  garage is the parent to tools.  Hopefully you are not confused at this point. I read a thread where you said not to use MovieClip(parent) so I'm appealing to you to show me how to use dispatch event to target these buttons from the main timeline.  Here is the link to the fla.

CS4 version

http://clienttestsite.x10.mx/beta/GARAGE.fla

Thanks in advance 

Translate
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
LEGEND ,
Jul 30, 2009 Jul 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(?);

Translate
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 ,
Jul 30, 2009 Jul 30, 2009

that's the loader.  one more parent.

Translate
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
LEGEND ,
Jul 30, 2009 Jul 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.

Translate
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
Engaged ,
Jul 30, 2009 Jul 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

Translate
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