Skip to main content
Known Participant
May 12, 2009
Answered

need help on calling an function on parent swf! please help!!

  • May 12, 2009
  • 1 reply
  • 1300 views

Hi, new to actionScript and trying to finish my final project, and i'm banging my head on a brick wall now, my question may be easy for some pros, so please help!!!

basically i've loaded an external swf from a button at main.swf

function loadSWF( e:MouseEvent ):void {

    var url:String;
    var buttonPressed:Sprite = e.currentTarget as Sprite;
   
   
    if( buttonPressed == button1){
        url = "./content1.swf"

     }

}

this works fine.

then i'm trying to disable the button function while the swf is loaded.  I add another eventListener to the same button.

function disableMenu( e:MouseEvent ):void {
        button1.mouseEnabled = false;

}

that works too.  ok now comes the problem.  I have a close button on my child swf("content1.swf) to close itself and returen to main swf.

but I want the  button on main.swf to work again.  so at the close button I add another eventListener hope to enable the menu.

(note this is on the child swf!)

function enableMenu ( e:MouseEvent ):void{

     root.button1.mouseEnabled = true;

}

I thought the root will take me back to the main swf but apparently it doesn't.  I keep on getting the error msg of button 1 is not defined.

Any idea how do I resolve this?  Or am I not supposedd to do it this way at all?  Sorry I'm a newb to AS still....

Any help will be appriciated!!! thanks in advance!

This topic has been closed for replies.
Correct answer

thanks for your help kglas

well on the timeline taht contain my button 1

i got output like this 

main [object MainTimeline]

and on the loaded swf i trace out this

[object MainTimeline]


Rather than reaching up to the topmost timeline, I think a much simpler approach would be to dispatch a custom event from the loaded swf.

In the child swf:

function enableMenu ( e:MouseEvent ):void{

     dispatchEvent(new Event("requestEnableMenu", true));

}

In the parent swf:

addEventListener("requestEnableMenu", enableMenu)

function enableMenu ( e:Event ):void{

     button1.mouseEnabled = true;

}

1 reply

kglad
Community Expert
Community Expert
May 12, 2009

if button1 is on the main timeline of your main swf, that code should work but you may need to cast the root as a movieclip:

MovieClip(root).button1.mouseEnabled = true;

jhintwAuthor
Known Participant
May 12, 2009

Thanks for the reply, but nope, still doesn't work.   The swf play ok but when I click on the button that close the external swf, the swf closes, but the buttons on main swf are still dead.  I got this error message.

TypeError: Error #1010: A term is undefined and has no properties. 

it keeps telling me that my button1 is undefined.

this child swf is loaded from xml.  and the close button is created by using AS file. The "button1" is just on the stage of the main swf.  Maybe these make some difference so i can't just point it back using (root)?

kglad
Community Expert
Community Expert
May 12, 2009

on your main swf's main timeline add:

trace(button1);

var testS:String = "test";

and in your loaded swf add:

trace(MovieClip(root).testS,MovieClip(root).button1);

what output do you see?