[Resolved] reusing eventlistener, is it possible?

Hey all, sorry if the question doesn’t belong here but what I am trying to do is draw rectangles on screen and read user input for them.

They represent numbers 0-9 on a phone, so I want to (like when you are about to make a call and input your numbers, think iPhone the phone graphical user interface) read what the user has pressed, instead of using a textbox and popping up the keyboard.

so I do something like: local arguments = { { id= 1, x= 5, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha}, { id= 2, x= 218, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha}, { id= 3, x= 430, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha},
… and so on for all 10 of the digits (0-9)

then I do a loop:

 for \_,item in ipairs( arguments ) do  
  
 -- btn attributes set here   
 btnPressed = display.newRect(item.x, item.y, item.width, item.height);  
 btnPressed.id = item.id; -- this should do it, right?  
  
 -- Add the event listener directly to an object  
 btnPressed:addEventListener("touch", btnPressed); -- this only works for the last entry in arguments-table, in my case digit 0  
 end  

and the event:

function btnPressed:touch(e) -- e represents the event to be handled   
 -- which button was pressed?  
 print(e.target.id);  
end;  

Can I even do that, ie pass along the id of the button(rectangle) I am touching?

Regards
BK
[import]uid: 144955 topic_id: 32284 reply_id: 332284[/import]

A quick fix for your code :slight_smile:

[code]
local btnWidth = 100
local btnHeight = 100

local arguments =
{
{ id= 1, x= 5, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha},
{ id= 2, x= 218, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha},
{ id= 3, x= 430, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha}}

local numTouch = function (event )
if event.phase == “began” then
print(event.target.id)
end
end;

for _,item in ipairs( arguments ) do

– btn attributes set here
btnPressed = display.newRect(item.x, item.y, item.width, item.height);
btnPressed.id = item.id; – this should do it, right?
– Add the event listener directly to an object
btnPressed:addEventListener(“touch”, numTouch); – this only works for the last entry in arguments-table, in my case digit 0
end

[/code] [import]uid: 81188 topic_id: 32284 reply_id: 128412[/import]

Thanks a lot, jkrassman. It worked out just great.
That proves that I need to learn Lua first before attempting to do anything of significance. :slight_smile:

Regards
BK [import]uid: 144955 topic_id: 32284 reply_id: 128417[/import]

No problems, you are learning while your are coding - thats the magic with programing :slight_smile:

Joakim [import]uid: 81188 topic_id: 32284 reply_id: 128437[/import]

A quick fix for your code :slight_smile:

[code]
local btnWidth = 100
local btnHeight = 100

local arguments =
{
{ id= 1, x= 5, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha},
{ id= 2, x= 218, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha},
{ id= 3, x= 430, y=194, width=btnWidth, height=btnHeight, alpha= btnDefaultAlpha}}

local numTouch = function (event )
if event.phase == “began” then
print(event.target.id)
end
end;

for _,item in ipairs( arguments ) do

– btn attributes set here
btnPressed = display.newRect(item.x, item.y, item.width, item.height);
btnPressed.id = item.id; – this should do it, right?
– Add the event listener directly to an object
btnPressed:addEventListener(“touch”, numTouch); – this only works for the last entry in arguments-table, in my case digit 0
end

[/code] [import]uid: 81188 topic_id: 32284 reply_id: 128412[/import]

Thanks a lot, jkrassman. It worked out just great.
That proves that I need to learn Lua first before attempting to do anything of significance. :slight_smile:

Regards
BK [import]uid: 144955 topic_id: 32284 reply_id: 128417[/import]

No problems, you are learning while your are coding - thats the magic with programing :slight_smile:

Joakim [import]uid: 81188 topic_id: 32284 reply_id: 128437[/import]