alstrup / arctic

Automatically exported from code.google.com/p/arctic
0 stars 0 forks source link

Button's click function is void -> void #13

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. make a loop to make some Buttons
2. Make an onclick function
3. How can I tell which button I clicked on in the onclick function?

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?
1.1.1

Please provide any additional information below.

Original issue reported on code.google.com by nik...@gmail.com on 15 May 2010 at 4:05

GoogleCodeExporter commented 9 years ago
Basically you need to use a different function for each button. There is a 
couple ways you can do this.
If you are looping through an array of data to create the buttons you could add 
the functions to that/an array also. eg.
{{{
for (i in 0...5) 
{ 
    buttons[i] = Button(up[i], over[i], buttonClickFun[i]);
}
}}}

or you can create an individual function inside the loop that calls your single 
onClick function with a unique id or something eg.
{{{
var self = this;
for (i in 0...5) 
{ 
    var buttonId = i; 
    buttons[i] = Button(up[i], over[i], function () { self.onButtonClick(buttonId); } );
}
}}}
You need to create the extra variable (buttonId) inside the loop as the Id, it 
won't work properly if you use i directly (because it keeps changing in 
subsequent iterations)
Hope that helps you out (sorry if the code formatting is bad, first time i've 
tried posting code in a comment)

Original comment by lyndon.h...@yahoo.com.au on 14 Jul 2010 at 2:42

GoogleCodeExporter commented 9 years ago
Thanks very much! 
I was missing the extra variable inside the loop.

Original comment by nik...@gmail.com on 19 Jul 2010 at 12:35