PDA

View Full Version : need help on calling an function on parent swf! please help!!



jhintw
May 12th, 2009, 03:15 AM
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.




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)?

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!

ThinkKirupa
May 12th, 2009, 07:30 AM
If you post your files I can take a look and hopefully help.


Tip. For ActionScript code hinting embed your code in these tags [as.] (without the dot)
trace("AS code");[/code]

Shaedo
May 12th, 2009, 09:11 PM
First you should put the following code to trigger in your child swf:


function enableMenu ( e:MouseEvent ):void
{
dispatchEvent(new Event("enableParentButton", true));
}


This will dispatch a custom "enableParentButton" event which will move up the hierachy and trigger any event listners that are on the lookout for "enableParentButton" events.
So obviously we need to create an event listner in the parent that will trigger when the custom "enableParentButton" event shows up. We add it to the loader and the code will look like this



loader.addEventListener("enableParentButton", resetButton);
function resetButton(event:Event):void
{
//add code to reset button here
}


your Loader may not be called loader so you will probably have to change this.

good luck!