# Confused with variable scope, or how to generate buttons

**URL:** https://forum.kirupa.com/t/confused-with-variable-scope-or-how-to-generate-buttons/167647
**Category:** Uncategorized
**Created:** [October 30, 2005, 5:48pm UTC](https://forum.kirupa.com/t/confused-with-variable-scope-or-how-to-generate-buttons/167647 "2005-10-30T17:48:54Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![pixeline](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pixeline/32/885_2.png) [@pixeline](https://forum.kirupa.com/u/pixeline)
#### Post date: [October 30, 2005, 5:48pm UTC](https://forum.kirupa.com/t/confused-with-variable-scope-or-how-to-generate-buttons/167647/1 "2005-10-30T17:48:54Z")

</div>

hello !

i’m having a hard time generating a menu from an mc in my library, that contains only a button and an empty textfield. The textfield instance name is myLabel and the button instance name is myButton.

Question1: when scripting the button behaviors, should i address the button or the containing mc ? I tried both, targeting the mc gives better results, but i find this illogical: according to the manual, you’re supposed to target the button AFAIU.

Question2: the values of the array “lang” are correctly displayed, except from within the onRollOver event. If i trace it, it indeed does not find the value. If i put the trace out of the function, it works correctly. So how can i access an array defined on the main timeline from within that function ? i tried \_root.lang and \_global.lang but no success… “undefined”

So, here is the code, i hope someone can tell me what i’m doing wrong:

[AS]  
// generate the main bot textfield  
var bot\_txt:TextField = this.createTextField(“bot\_txt”, this.getNextHighestDepth(), 10, 5, 220, 25);  
bot\_txt.multiline = true;  
bot\_txt.wordWrap = true;  
bot\_txt.text = “Please choose your language :”;  
bot\_txt.setTextFormat(speakbox\_fmt);  
// generate the language buttons  
for (i=0; i\<lang.length; i++)  
{  
var my\_button\_mc:String = lang\*[0]+"\_butt";  
var ypos:Number = 40+i_20;  
var butt:MovieClip = this.attachMovie(“lang\_button”, my\_button\_mc, i, {\_x:100, \_y:ypos});  
// sets the button"s label  
this.butt.myLabel.text = lang_[1];  
trace (lang\*[2]);  
this.butt.myLabel.setTextFormat(labelTxt\_fmt);  
//sets the button"s behaviors  
this.butt.onRollOver = function()  
{  
//trace (\_level0.lang\*[0]);  
this.myLabel.setTextFormat(rollOverTxt\_fmt);  
var val:String = lang\*[0];  
bot\_txt.text = val;

```
};
this.butt.onRollOut = function()
{
    this.myLabel.setTextFormat(labelTxt_fmt);
};

```

}  
stop();  
[/AS]
