# Assigning button actions within a loop

**URL:** https://forum.kirupa.com/t/assigning-button-actions-within-a-loop/153363
**Category:** flash
**Created:** [June 30, 2005, 9:56pm UTC](https://forum.kirupa.com/t/assigning-button-actions-within-a-loop/153363 "2005-06-30T21:56:49Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![stephenr85](https://avatars.discourse-cdn.com/v4/letter/s/a183cd/32.png) [@stephenr85](https://forum.kirupa.com/u/stephenr85)
#### Post date: [June 30, 2005, 9:56pm UTC](https://forum.kirupa.com/t/assigning-button-actions-within-a-loop/153363/1 "2005-06-30T21:56:49Z")

</div>

This function all works correctly, except where i am assigning an action to the onRelease event of a given button. Every button is assigned to the same value (appears to be the first value pulled from the array). Pretty frustrating…help?!

function createSubItems() {  
vspacer = 6;  
i = 0;  
\_global.subHeights = [];  
for (j=1; j\<=7; j++) {  
subOf = “item”+j+"\_sub";  
subName = eval(\_root.subOf);  
hotspot = subName.hotspot;  
linkArea = subName.linkArea;  
colorArea = subName.colorArea;  
totalTextHeight = 0;  
totalTextWidth = 0;  
lvl = 0;  
spacer2 = 4;  
//create links  
tracker = 0;  
while (xmlSubLinks\* != “endsection”) {  
createLinkText(xmlSubLinks\*, subName, subOf, lvl, 0x000000);  
if (clipName.\_width\>totalTextWidth) {  
totalTextWidth = clipName.\_width;  
}  
\_global.level++;  
clipName.\_y = vspacer+totalTextHeight;  
clipName.\_x = colorArea.\_x+8;  
totalTextHeight += 20;  
//increment  
i += 2;  
lvl++;  
tracker++;  
}  
i += 2;  
//size & position subName  
if (tracker\>0) {  
linkArea.\_height = totalTextHeight+1;  
linkArea.\_width = (spacer-spacer2)+totalTextWidth+(spacer-spacer2);  
colorArea.\_width = linkArea.\_width+(7);  
colorArea.\_height = totalTextHeight+(spacer2\*2)+1;  
\_global.subHeights[j] = colorArea.\_height;  
colorArea.\_y = 0;  
colorArea.\_x = 0;  
linkArea.\_y = colorArea.\_y+spacer2;  
linkArea.\_x = colorArea.\_x+spacer2;  
}  
//draw stuff for the links  
bottomOfLink = spacer2;  
for (k=0; k\<tracker; k++) {  
trace(xmlSubLinks[k+1]);  
bottomOfLink += 20;  
subName.createEmptyMovieClip(“line”+k, k);  
subName.createEmptyMovieClip(“link”+k, k+tracker);  
//draw line  
eval(subName+".line"+k).lineStyle(1, 0xA3A19E, 100);  
eval(subName+".line"+k).moveTo((linkArea.\_x+linkArea.\_width)-1, bottomOfLink);  
if (k\<tracker-1) {  
eval(subName+".line"+k).lineTo(linkArea.\_x, bottomOfLink);  
}  
//duplicate and move button  
subName.linkBox.duplicateMovieClip(“linkBox”+k, -tracker+k);  
linkBoxName = eval(subName+".linkBox"+k);  
linkBoxName.\_height = 18.8;  
linkBoxName.\_y = bottomOfLink-19;  
linkBoxName.\_x = linkArea.\_x+.75;  
linkBoxName.\_width = linkArea.\_width-2.5;  
linkBoxName.\_alpha = 0;  
//create button actions  
fadeSpeed = 35;  
linkBoxName.onRollOver = function() {  
this.onEnterFrame = function() {  
if (this.\_alpha\<100) {  
this.\_alpha += fadeSpeed;  
}  
};  
};  
linkBoxName.onRollOut = function() {  
this.onEnterFrame = function() {  
if (this.\_alpha\>0) {  
this.\_alpha -= fadeSpeed;  
}  
};  
};  
linkBoxName.onRelease = function() {  
getURL(xmlSubLinks[k+1]);  
trace(xmlSubLinks[k+1]);  
};  
}  
//position main area  
subName.\_y = StageHeight;  
subName.\_x = eval(“boxes.item”+j+"\_box").\_x;  
if (subName.\_x+colorArea.\_width\>StageWidth) {  
subName.\_x -= colorArea.\_width;  
if (j != total) {  
subName.\_x += (eval(“boxes.item”+(j+1)+"\_box")).\_x-(eval(“boxes.item”+(j)+"\_box")).\_x;  
} else {  
subName.\_x += StageWidth-(eval(“boxes.item”+(j)+"\_box")).\_x;  
}  
}  
subName.linkBox.\_visible = false;  
subName.\_visible = false;  
}  
}
