Hi all!
Quick question. I am creating an app that uses a grid of buttons. I set the OnTouch handler to a closure, which calls my button handler with the array indexes of the button touched. Problem is, each time my handler gets called with the same arguments, the highest I and J in my loop. The closure is not preserving its arguments. I have looked on other sites, and it appears I coded this correctly, comparing it to javascript for the web. Just curious, am I missing something, or is this a droidscript bug/feature? Of course, only the features that get fixed in an update were retroactively bugs, so I've always believed.
The code is below. Not everything is there to keep it simple, just the stuff worth looking at. I found another solution, I added an object to each button with its array indexes, just want to learn for future coding.
Thanks in advance.
//code and psudocode
//inside the OnStart function:
btns = [];
//create vertical layout, vlay.
//my loops:
for (var i = 0; i < 4; i++) {
//create horizontal layout, hlay, and add to vlay.
btns[i] = []; //new row.
for (j = 0; j < 4; j++) {
btns[i][j] = app.CreateButton(...).
//*** the questionable code:
btns[i][j].SetOnTouch(((function(x,y) {
return function() {
btnTouched(x,y); //BUG, always calls with args (3, 3), no matter the button.
}//inner function (the handler)
})(i, j)); //touchHandler
//add the button to hlay.
} //j
} //i
//remainder of app.
Looking forward to your answers!