Attaching onRollOvers in a for loop?

Hi people,

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


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!

try

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

[QUOTE=stringy;2358266]try

_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…

[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

_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]
			};

a prettier way:

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);
			};
		}
	}
}