table + event listener

Hi…

I’m trying to create a little numeric keyboard, but i’ve a problem with event/listener.

I create numeric button in a double cycle for and i insert those buttons in an event listener., but when i press every buttons the result is always 9 and not the real numeric value of the button. 

Why that? Where is my error? There is another method to insert a object in an avenue listener?

Here i allegate my code.

 local buttons = {} buttons[1] = {num = "1", id = "1"} buttons[4] = {num = "2", id = "2"} buttons[7] = {num = "3", id = "3"} buttons[2] = {num = "4", id = "4"} buttons[5] = {num = "5", id = "5"} buttons[8] = {num = "6", id = "6"} buttons[3] = {num = "7", id = "7"} buttons[6] = {num = "8", id = "8"} buttons[9] = {num = "9", id = "9"} local value = display.newText("0",display.contentCenterX,display.contentCenterY - 130,native.systemFontBold,15) local text = {} for i = 1,3 do for j = 1,3 do local bg = display.newRect( display.contentCenterX - 100 + i \*50,display.contentCenterY - 69 + j\*50, 40, 40 ) bg:setFillColor( 1,0,0 ) text[#text+1] = display.newText( buttons[#text+1].num, display.contentCenterX - 100 + i \*50,display.contentCenterY - 69 + j\*50,native.systemFontBold,14 ) text[#text]:setFillColor( 0,1,0 ) local function listener() print (text[#text].text) end text[#text]:addEventListener( "touch", listener ) end end 

Thanks for your helps.

It’s because of this line here:

print (text[#text].text)

Everytime the function is called it looks up the length of the table “text” as it is at the point where the function is called, which is 9. That means that it’s not going to refer to the object that called it, it’s going to refer to the last element that was put into the text table. 

There are a few ways around this. The simplest is to use the .text property of whatever object called the function using event.target :

local function listener(event) print(event.target.text) end text[#text]:addEventListener( "touch", listener )

I would advise using a touch phase so that it doesn’t trigger the print when the numpad touch begins, moves and ends. Most likely you only want to do anything on the began or ended phase:

local function listener(event) if event.phase == "ended" then print(event.target.text) end end

It’s because of this line here:

print (text[#text].text)

Everytime the function is called it looks up the length of the table “text” as it is at the point where the function is called, which is 9. That means that it’s not going to refer to the object that called it, it’s going to refer to the last element that was put into the text table. 

There are a few ways around this. The simplest is to use the .text property of whatever object called the function using event.target :

local function listener(event) print(event.target.text) end text[#text]:addEventListener( "touch", listener )

I would advise using a touch phase so that it doesn’t trigger the print when the numpad touch begins, moves and ends. Most likely you only want to do anything on the began or ended phase:

local function listener(event) if event.phase == "ended" then print(event.target.text) end end