# Problem with recursive menu and variable scope with mouseovers

**URL:** <https://forum.kirupa.com/t/problem-with-recursive-menu-and-variable-scope-with-mouseovers/294025>\
**Category:** flash\
**Created:** [August 7, 2009, 5:03am UTC](https://forum.kirupa.com/t/problem-with-recursive-menu-and-variable-scope-with-mouseovers/294025 "2009-08-07T05:03:10Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![mlee](https://avatars.discourse-cdn.com/v4/letter/m/7cd45c/32.png) [@mlee](https://forum.kirupa.com/u/mlee)\
**Post date:** [August 7, 2009, 5:03am UTC](https://forum.kirupa.com/t/problem-with-recursive-menu-and-variable-scope-with-mouseovers/294025/1 "2009-08-07T05:03:10Z")

</div>

Long time listener, first time poster… I solved a similar problem before in AS2, but can’t figure out the solution to this issue.

In the code below, I’m looping through nested xml structure of an unknown number of levels. GenMenu is (or will be) a recursive function that will build each level of the menu. My problem is that I can’t figure out how to reference the variables from an eventlistener function. I need to pass the children node\_xml\* to the next recursion, as well as set hover styles on the menu links, etc…

I’ve tried several hours of google searches and forum crawling, but can’t find a solution that makes sense. It’s probably something simple, but I’m just not finding it. Any ideas? Thanks.

```auto

function GenMenu(container, name, x, y, depth, node_xml) {
    // variable declarations
    var curr_node = new Array();
    var curr_menu:MovieClip = new MovieClip();
    addChild(curr_menu);

    // for all items and submenus within this node_xml passed for this menu
    for (var i=0; i<node_xml.length(); i++) {
        curr_menu["item_" + i] = new MovieClip();
        curr_menu["item_" + i].x = x;
        curr_menu["item_" + i].y = y - i*20;
        curr_menu["item_" + i].trackAsMenu = true;
        curr_menu.addChild(curr_menu["item_" + i]);
        
        var myText:TextField = new TextField();
        myText.text = node_xml*.@title;
        myText.name = "myText" + i;
        myText.autoSize = TextFieldAutoSize.LEFT;
        curr_menu["item_" + i].addChild(myText);

        curr_menu = node_xml*;
        trace(curr_menu + "this traces correctly...");
    
        curr_menu["item_" + i].addEventListener(MouseEvent.CLICK, myButtonClick);
        function myButtonClick(ev:MouseEvent):void {
            trace("doesn't trace correctly - how do you access node_xml* here? (or any variable for that matter)")
        }
    }
}

```
