# Attaching onRollOvers in a for loop?

**URL:** <https://forum.kirupa.com/t/attaching-onrollovers-in-a-for-loop/265902>\
**Category:** flash\
**Created:** [July 14, 2008, 1:35pm UTC](https://forum.kirupa.com/t/attaching-onrollovers-in-a-for-loop/265902 "2008-07-14T13:35:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![CoDe\_ReD](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/code_red/32/336_2.png) [@CoDe\_ReD](https://forum.kirupa.com/u/CoDe_ReD)\
**Post date:** [July 14, 2008, 1:35pm UTC](https://forum.kirupa.com/t/attaching-onrollovers-in-a-for-loop/265902/1 "2008-07-14T13:35:14Z")

</div>

Hi people,

I’m having a bit of a problem with attaching some links to a series of buttons I dynamically create.

```auto

function createSocks() {
	_root.createEmptyMovieClip("socksSeries", this.getNextHighestDepth());
	for (p=0; p<seriesNode.length; p++) {
		var imagesNode = seriesNode[p].firstChild.childNodes;
		_root.socksSeries.createEmptyMovieClip("series"+p, _root.socksSeries.getNextHighestDepth());
		_root.socksSeries["series"+p]._y = 400;
		_root.socksSeries["series"+p]._x = 100 + (p*100);
		//var seriesIDNode = mainnode.childNodes[0].childNodes[p].attributes.seriesID;
		for (j=0; j<imagesNode.length; j++) {
			var imagesNode2 = seriesNode[p].firstChild.childNodes[j].attributes.valueSeries;
			_root.socksSeries["series"+p].createEmptyMovieClip("sock"+j, _root.socksSeries["series"+p].getNextHighestDepth());
			_root.socksSeries["series"+p]["sock"+j].createEmptyMovieClip("container", 0);
			loadMovie(imagesNode2, _root.socksSeries["series"+p]["sock"+j].container);
			_root.socksSeries["series"+p]["sock"+j].attachMovie("testbutton", "button", 1);
			_root.socksSeries["series"+p]["sock"+j].button._x = 6;
			_root.socksSeries["series"+p]["sock"+j].button._y = 5;
			_root.socksSeries["series"+p]["sock"+j]._x = (100*j);
			_root.socksSeries["series"+p]["sock"+j]._y = 0;
			_root.socksSeries["series"+p]["sock"+j].button.onRollOver = function() {
				trace("Button " + j);
			}
		}
	}
}

```

I use an XML file that contains links to images. That all works fine. What I want is to attach an action to every button that I create with this loop, and I want it to be different on every button. So sock0 in series0 should trace out “Button 0”. Intead it completes the entire for loop and it traces out the last value j has on every sock (all socks trace “Button 4”).

Is this possible? Perhaps with an extra function?

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![stringy](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@stringy](https://forum.kirupa.com/u/stringy)\
**Post date:** [July 14, 2008, 1:49pm UTC](https://forum.kirupa.com/t/attaching-onrollovers-in-a-for-loop/265902/2 "2008-07-14T13:49:05Z")

</div>

try

```auto
_root.socksSeries["series"+p]["sock"+j].button.j = j;
_root.socksSeries["series"+p]["sock"+j].button.onRollOver = function() {
	trace("Button "+this.j);
};

```

---

<div class="post-metadata">

**Author:** ![CoDe\_ReD](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/code_red/32/336_2.png) [@CoDe\_ReD](https://forum.kirupa.com/u/CoDe_ReD)\
**Post date:** [July 14, 2008, 2:01pm UTC](https://forum.kirupa.com/t/attaching-onrollovers-in-a-for-loop/265902/3 "2008-07-14T14:01:37Z")

</div>

[QUOTE=stringy;2358266]try

```auto
_root.socksSeries["series"+p]["sock"+j].button.j = j;
_root.socksSeries["series"+p]["sock"+j].button.onRollOver = function() {
	trace("Button "+this.j);
};

```

[/QUOTE]

Ah yes, that works! Thanks so much for that.

I did notice another problem though. The button I lay over the image is duplicated everytime I create a new sock movieclip. What I mean is that in the first sock movieclip there is one button, in the second sock movieclip there are two buttons and in the third sock movieclip there are three buttons. The problem is that these movieclips all have different actions applies to them if I’m correct. I can’t see the error in my code…

---

<div class="post-metadata">

**Author:** ![stringy](https://avatars.discourse-cdn.com/v4/letter/s/919ad9/32.png) [@stringy](https://forum.kirupa.com/u/stringy)\
**Post date:** [July 14, 2008, 2:28pm UTC](https://forum.kirupa.com/t/attaching-onrollovers-in-a-for-loop/265902/4 "2008-07-14T14:28:13Z")

</div>

[QUOTE=CoDe-ReD;2358279]Ah yes, that works! Thanks so much for that.

I did notice another problem though. The button I lay over the image is duplicated everytime I create a new sock movieclip. What I mean is that in the first sock movieclip there is one button, in the second sock movieclip there are two buttons and in the third sock movieclip there are three buttons. The problem is that these movieclips all have different actions applies to them if I’m correct. I can’t see the error in my code…[/QUOTE]

I`m not really sure what you mean  
maybe something like this

```auto
_root.socksSeries["series"+p]["sock"+j].button.p = p;
			_root.socksSeries["series"+p]["sock"+j].button.j = j;
			_root.socksSeries["series"+p]["sock"+j].button.onRollOver = function() {
				trace("Button "+this.p+this.j);
				//so maybe in a 2d array reference with myarray[this.p][this.j]
			};

```

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [July 14, 2008, 2:44pm UTC](https://forum.kirupa.com/t/attaching-onrollovers-in-a-for-loop/265902/5 "2008-07-14T14:44:09Z")

</div>

a prettier way:

```auto
var root:MovieClip = this
function createSocks():Void {
	var parentSeries:MovieClip = root.createEmptyMovieClip("socksSeries", root.getNextHighestDepth());
	for (var p:Number = 0; p<seriesNode.length; p++) {
		var imagesNode = seriesNode[p].firstChild.childNodes;
		var series:MovieClip = parentSeries.createEmptyMovieClip("series"+p, parentSeries.getNextHighestDepth());
		series._y = 400;
		series._x = 100+(p*100);
		//var seriesIDNode = mainnode.childNodes[0].childNodes[p].attributes.seriesID;  
		for (var j:Number = 0; j<imagesNode.length; j++) {
			var imagesNode2:XMLNode = seriesNode[p].firstChild.childNodes[j].attributes.valueSeries;
			var sock:MovieClip = series.createEmptyMovieClip("sock"+j, series.getNextHighestDepth());
			var container:MovieClip = sock.createEmptyMovieClip("container", 0);
			var button:MovieClip = sock.attachMovie("testbutton", "button", 1);
			container.loadMovie(imagesNode2);
			sock._x = (100*j);
			sock._y = 0;
			button.xmlID = p;
			button.id = j;
			button._x = 6;
			button._y = 5;
			button.onRollOver = function():Void {
				trace("Button p:"+this.xmlID);
				trace("Button j:"+this.id);
			};
		}
	}
}

```
