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

Trying to call a root level function from within a loaded swf

Community Beginner ,
Aug 20, 2014 Aug 20, 2014

I'm not very familiar with AS3... I'm trying to have a loaded swf file call a function from the parent to unload itself when it comes to the end (on the timeline, not as a button). I'm assuming I need to be calling the function (that is used for the close button) from the loaded swf (perhaps I'm mistaken though?).

Here's the code from one of my buttons that loads an swf. I'm guessing I need to call the closeSWF function from the timeline of my loaded movie but having no luck figuring out how to do that.

Any help would be appreciated.

// 01 ------------------------------------------------------

function loadWeaving(ev:MouseEvent):void {

  // load SWF

  var movieClip:MovieClip = new holder_mc;

  //add it to the stage

  addChild(holderSWF);

  holderSWF.x=0;

  holderSWF.y=0;

  //

  var requestEN:URLRequest = new URLRequest("WhatIsWeaving_Eng.swf"); // english

  holderSWF.load(requestEN);

 

  //

  close_bt.addEventListener(MouseEvent.CLICK, closeSWF);

  function closeSWF(infoObject:Object):void {

  trace("close SWF");

  removeChild(holderSWF);

  // reset switch

  close_bt.visible = false;

  showAllButtons();

  infoObject.currentTarget.removeEventListener(infoObject.type, closeSWF);

  }

  //

  setChildIndex(close_bt,numChildren - 1);

  close_bt.visible = true;

  hideAllButtons();

  // - start timer last

  myTimer.addEventListener(TimerEvent.TIMER, closeSWF);

}

TOPICS
ActionScript
1.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
Participant ,
Aug 20, 2014 Aug 20, 2014

Honestly, I didn't exactly follow all of that because I'm a bit pressed for time.  But if I had an SWF (which I was going to be importing at runtime) and Iw anted a function to be called from it's timeline, I would use something like (this.parent as MovieClip).functionName();

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
Participant ,
Aug 20, 2014 Aug 20, 2014

Ah okay.  It would be something like this:

(this.parent as MovieClip).closeSWF(myObject);


If the script were on a timeline within a MovieClip on the timeline of your imported SWF, it would be this:

(this.parent.parent as MovieClip).closeSWF(myObject);

and so on........................

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 ,
Aug 20, 2014 Aug 20, 2014

i assume holderSWF is a loader class object.  if so, its content (cast as a movieclip) will reference the loaded swf's main timeline.  if your function is on that loaded swf's main timeline, after loading is complete you can use:

MovieClip(holderSWF.content).yourloadedswfsfunction();

p.s. here's an adobe kb article i wrote on swf-to-swf communication,  http://kb2.adobe.com/community/publishing/918/cpsid_91887.html

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 Beginner ,
Aug 21, 2014 Aug 21, 2014

I think maybe I'm going about this the wrong way... Should I be using some kind of event listener in the main swf which would trigger when the loaded swf file comes to a specific frame (that I designate as the end)? Still beyond my AS3 grasp, but seems like the way to tackle it so it's done from within my container swf instead of from the loaded swf...

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 ,
Aug 21, 2014 Aug 21, 2014

you can but you'll still need to use the content property of your loader.

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
Participant ,
Aug 21, 2014 Aug 21, 2014

Seriously, I use this:  "(this.parent as MovieClip).functionName(); (or (this.parent.parent... as MovieClip).functionName();)  all the time to call a function from the timeline of an imported swf.

It works!  🙂

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 ,
Aug 21, 2014 Aug 21, 2014

that won't work with a loaded swf.

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
Participant ,
Aug 21, 2014 Aug 21, 2014

Yes it does.  It may not be "best practice" but it does work. 

(It may require more than one "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
Community Expert ,
Aug 21, 2014 Aug 21, 2014

you must be calling a loading swf function from the loaded swf AFTER the loaded swf is added to the stage.

to call a loaded swf function from the loading swf, use the content property of the loader.

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
Guide ,
Aug 21, 2014 Aug 21, 2014

You're exactly right. If you attach an event listener to the Loader before you add it to the stage, you can then use event.currentTarget to refer to the loader. And this is definitely the better way to do it if you're planning to use the loaded swf from multiple places or could ever need to do this in the future.

So:

protected function initLoader():void {

     var loader:Loader = new Loader();

     loader.addEventListener('closeMe', closeLoader);

     loader.load(new URLRequest(...));

     addChild(loader);

}

protected function closeLoader(e:Event):void {

     var loader:Loader = e.currentTarget as Loader;

     if (loader) {

          loader.unloadAnstop(true);

          loader.removeEventListener('closeMe', closeLoader);

          removeChild(loader);

     }

}

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
Participant ,
Aug 21, 2014 Aug 21, 2014

I don't see how that's "definitely the better way to do it" but ok.  🙂

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
Guide ,
Aug 22, 2014 Aug 22, 2014

Google for "loose coupling," "principle of least knowledge," and "law of Demeter." Possibly that will help.

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
Participant ,
Aug 22, 2014 Aug 22, 2014

When they started pushing OOP is when Flash Pro started going downhill IMHO. 

(Please refer to "Why is flash Pro being Deprecated?" for more of my heretical opinions on this topic.) 😉

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
Guide ,
Aug 22, 2014 Aug 22, 2014

You also say your work is limited to prototypes. Just because you don't get how good design helps with the maintainability of something that may be around for years doesn't mean you should go around derailing others who want to do things in a way that will make changes easier on a large project.

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
Participant ,
Aug 22, 2014 Aug 22, 2014

😉

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
Enthusiast ,
Aug 25, 2014 Aug 25, 2014

>>When they started pushing OOP is when Flash Pro started going downhill IMHO.


Your opinion is wrong. Unless you're making very small applications proper structure, ie OOP, is necessary for organizing your project. AS3 added tools for serious developers - which you don't even need to use, you can have poorly structured timeline code if you want still.

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
Participant ,
Aug 25, 2014 Aug 25, 2014

I would never argue against the idea that OOP is by far the better way to go for large-scale projects - especially those with multiple team members.  But, if the project in question is NOT a large-scale project, then I still think my way is easier.  🙂

I regret I must hold to my position on Flash.  Since the program was not originally intended for "serious developers", you will excuse me for expressing my grievances that I, a loyal Flash user since 1997 (and a Director user before that since 1990), feel a little abandoned due to the direction they seem to be taking Flash Pro.  😞

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
Guide ,
Aug 25, 2014 Aug 25, 2014

Yes, let's keep all software exactly as it was imagined in 1996 . No shifts in direction are allowed.

I agree with you that Adobe dropped the ball on how it handled the transition to AS3, but it was strictly a focus/documentation problem. The actual changes they made in the software were the right thing to do.

The nice thing is that those of us who kept up are well-positioned to move right into other technologies such as AnglularJS. Maybe you can do cel animation after Flash as we know it is gone.

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
Participant ,
Aug 25, 2014 Aug 25, 2014

Well, since most "serious developers" no longer take Flash seriously, you tell me how good a call that "shift in direction" was....

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
Participant ,
Aug 25, 2014 Aug 25, 2014
LATEST

Personally, I hope Flash makes it, because as I've said, I still consider it far and away, the best mobile software prototyping tool available on the market.

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