# Keeping a variable unique for a dynamically created function

**URL:** https://forum.kirupa.com/t/keeping-a-variable-unique-for-a-dynamically-created-function/288924
**Category:** flash
**Created:** [May 23, 2009, 5:43am UTC](https://forum.kirupa.com/t/keeping-a-variable-unique-for-a-dynamically-created-function/288924 "2009-05-23T05:43:26Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![letterafterz](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@letterafterz](https://forum.kirupa.com/u/letterafterz)
#### Post date: [May 23, 2009, 5:43am UTC](https://forum.kirupa.com/t/keeping-a-variable-unique-for-a-dynamically-created-function/288924/1 "2009-05-23T05:43:26Z")

</div>

\*\*Hi guys,

Sorry if this is addressed elsewhere, I’ve been scouring the internet for over an hour but I’m not entirely sure even what to search for exactly.

What I’m trying to do is setup a function that shoots through a set of buttons within a subset and creates click code for these buttons.

THe three variables are the identifiers for the actual keyframe i wish to jump to, the movieclip in which i want to perform the action and the actual button to go to the right frame.

Presently the problem I’m having is that the variables aren’t stored when the onRelease functions are created, so they all are just calling the LAST variables declared.

Here’s a sample of the code I currently have

```auto

//function to load canvas/acrylic
function setSwatches(mySwatchCount:Number, myContainer:String)
{
    //set holder name
    var myContainerName = myContainer + "_mc"
    
    //cycle for each swatch
    for(var i = 0; i < mySwatchCount; i++){
        var mySwatchbtn = myContainer+ "_" + i + "_btn";
        var myFrameLabel = "a"+i; //adds an "a" because frame labels don't work with numbers

        //attach button code to load each swatch
        trace(myFrameLabel);
        
        this[mySwatchbtn].onRelease = function(){
            trace(myFrameLabel);
            _root[myContainerName].gotoAndStop(myFrameLabel);
        }
    }
}

//setup buttons
setSwatches(8, "fa_acrylic");
setSwatches(8, "fa_canvas");

```

Thanks guys!  
Steve  
Natural Cow Design  
[http://www.naturalcowdesign.com.au](http://www.naturalcowdesign.com.au)  
\*\*
