PDA

View Full Version : Calling stage function from nested MC



jmalko
September 12th, 2008, 10:48 AM
Hello all, this is my first time posting, but not my first visit to the site. I do have a problem.

I'm trying to get a set of dynamically generated MCs that are nested about 4 levels down to use a function defined in the main timeline.

Here's the main timeline code (parts not related are missing):



var domesticLangVar:String = new String();
domesticLangVar = "USA";

var invokeLanguage:MovieClip = new MovieClip();
invokeLanguage.addEventListener(Event.ENTER_FRAME, languageSelector);
addChild(invokeLanguage);

function languageSelector(event:Event):void {

trace(domesticLangVar);
invokeLanguage.removeEventListener(Event.ENTER_FRA ME, languageSelector);
this.topBar_mc.langpicker_mc.flagimgholder_mc.addC hild(flagimg);
}


Here is the button's code:



import flash.events.MouseEvent;

var nationLangVar:String;
var thumbImageVar:String;
var domesticLangVar:String;

var img = new Loader();
img.load(new URLRequest(thumbImageVar));
flag_img.addChild(img)


invisflag_btn.buttonMode = true;
invisflag_btn.mouseChildren = false;

invisflag_btn.addEventListener(MouseEvent.CLICK , flagbuttonClick);

function flagbuttonClick(event:MouseEvent):void {

domesticLangVar = nationLangVar;
(root as MovieClip).invokeLanguage.addEventListener(Event.E NTER_FRAME, languageSelector);
var sndClick:click_snd = new click_snd();
sndClick.play();
(root as MovieClip).topBar_mc.langpicker_mc.langpalette_mc. gotoAndPlay("flagsup");
(root as MovieClip).topBar_mc.langpicker_mc.flagarrow_direc tion.gotoAndStop(1);

}

invisflag_btn.addEventListener(MouseEvent.MOUSE_OV ER , flagbuttonOver);
invisflag_btn.addEventListener(MouseEvent.MOUSE_OU T , flagbuttonOut);

function flagbuttonOver(event:MouseEvent):void {
var sndOver:over_snd = new over_snd();
sndOver.play();
flaghighlight_mc.gotoAndStop(2);


}
function flagbuttonOut(event:MouseEvent):void {
flaghighlight_mc.gotoAndStop(1);

}


The reason I have the initial invokeLanguage event listener is because I have a detect via flashvars for the user's default language. I want the user to be able to change the language, but I don't want to have to have the function code in two places...that would be a mess. My thinking was to change the domesticLangVar and add the invokelanguage event listener again, but alas, the flash flayer is looking for the function in the nested button and can't find it. How do I point flash into the right direction?

jmalko
September 12th, 2008, 12:38 PM
Ok, I've actually got the function in the nested MC to talk to the main timeline and access the function. I got rid of the invokeLanguage thing all together.

Main Timeline:



var domesticLangVar:String = new String();
domesticLangVar = "USA";

languageSelector();

function languageSelector() {

trace(domesticLangVar);

}


the button click code:



function flagbuttonClick(event:MouseEvent):void {

domesticLangVar = nationLangVar;
trace(domesticLangVar + "!");
(root as MovieClip).languageSelector();
}


Now it is accessing the function just fine, but it is not updating my domesticLangVar so no change actually takes place. The trace on the button press works, but the trace on the main timeline still reads out USA. Any ideas?>

jmalko
September 18th, 2008, 11:51 AM
nobody knows?

Tegeril
September 18th, 2008, 01:47 PM
This is a scope issue.

You are changing a variable "domesticLangVar" in the button that has no relation to the variable on the main timeline. Also, casting root as a MovieClip is not really the best way to go (I believe the proper way is root[languageSelector](domesticLangVar) but I'm not positive because I have never needed to do this). That said, for your button:



function flagbuttonClick(event:MouseEvent):void {

domesticLangVar = nationLangVar;
trace(domesticLangVar + "!");
(root as MovieClip).languageSelector(domesticLangVar);
}


and for your timeline:



var domesticLangVar:String = new String();
domesticLangVar = "USA";

languageSelector(domesticLangVar);

function languageSelector(newLang:String) {

domesticLangVar = newLang;
trace(domesticLangVar);

}

jmalko
September 18th, 2008, 02:13 PM
Wow, thank you so much! This got me in the right direction. The only things I needed to change from your code were to substitute the domesticLangVar to something else within the function.

Main timeline:

var domesticLangVar:String = new String();
var currentLangVar:String = new String();
domesticLangVar = "DR";

languageSelector(domesticLangVar);


function languageSelector(newLang:String) {

currentLangVar = newLang;

trace(currentLangVar);

}



Button:



var newdomesticLangVar:String;

function flagbuttonClick(event:MouseEvent):void {

newdomesticLangVar = nationLangVar;
trace(newdomesticLangVar + "!");
(root as MovieClip).languageSelector(newdomesticLangVar);

}

Thank you so much again for your help. The result is fantastic!