Assigning one listener to multiple tap-events

Hi,

I tried for a while but I didn’t find a solution to this:

Say I have several small images on the screen. And i want them to print their position when they get tapped.

It’s easy for one single image:

[lua]circle = display.newCircle(20, 20, 20)

function circle:tap(event)
print(circle.x…"/"…circle.y)
end

circle:addEventListener(“tap”, circle)[/lua]

But what if I have something like this:

[lua]circle1 = display.newCircle(20, 20, 20)
circle2 = display.newCircle(60, 20, 20)
circle3 = display.newCircle(100, 20, 20)
circle4 = display.newCircle(140, 20, 20)
circle5 = display.newCircle(180, 20, 20)[/lua]

I don’t want to define five different listeners since they would all do the same. So is there a way to define the listener once and reuse it in multiple tap-events? [import]uid: 24216 topic_id: 5732 reply_id: 305732[/import]

Ok, got it.

[lua]local circleTapped = function(event)
local circle = event.target
print(“Tapped circle at: “…circle.x…”/”…circle.y)
end

circle1 = display.newCircle(20, 20, 20)
circle1:addEventListener(“tap”, circleTapped)
circle2 = display.newCircle(60, 20, 20)
circle2:addEventListener(“tap”, circleTapped)
circle3 = display.newCircle(100, 20, 20)
circle3:addEventListener(“tap”, circleTapped)
circle4 = display.newCircle(140, 20, 20)
circle4:addEventListener(“tap”, circleTapped)
circle5 = display.newCircle(180, 20, 20)
circle5:addEventListener(“tap”, circleTapped)[/lua]

Was too easy. :wink: [import]uid: 24216 topic_id: 5732 reply_id: 19631[/import]

Hi Fly,

I need to pass arguments to the function which is called when
an event is fired…

TRectangle1:addEventListener(“touch”,common)

Here i need to pass three args to the “common” function.

how can i do so.

i tried this

TRectangle1:addEventListener(“touch”,function(event){ common(“TRectangle1”,“GblRectPosX1”,“GblRectPosY1”) })

but it gives me an error…

Thankx
Krunal. [import]uid: 33757 topic_id: 5732 reply_id: 21633[/import]

I don’t know if there’s a better solution to this since I’m new to Lua, but I’m doing it like this in my code:

[lua]local common = function(event)
local rec = event.target – the object that triggerd the event
xpos = rec.GblRectPosX1
ypos = rec.GblRectPosY1

end

TRectangle1.GblRectPosX1 = xx
TRectangle1.GblRectPosY1 = yy

TRectangle1:addEventListener(“touch”,common)[/lua] [import]uid: 24216 topic_id: 5732 reply_id: 21678[/import]

Hi Fly,

Thankx for reply,

But i hv 5 rectangle and i wannt to work with only one Listener so no need to add extra Listener for another 4 like we hv make one click event for different button and as per sender object we find the id and do operation on same.

is it possible in Lua ?

Krunal [import]uid: 33757 topic_id: 5732 reply_id: 21680[/import]

But you can use the same listener on every of your rectangels:

[lua]local common = function(event)
local rec = event.target – the object that triggerd the event
xpos = rec.GblRectPosX
ypos = rec.GblRectPosY

end

TRectangle1.GblRectPosX = xx
TRectangle1.GblRectPosY = yy

TRectangle1:addEventListener(“touch”,common)

TRectangle2.GblRectPosX = xx
TRectangle2.GblRectPosY = yy

TRectangle2:addEventListener(“touch”,common)

TRectangle3.GblRectPosX = xx
TRectangle3.GblRectPosY = yy

TRectangle3:addEventListener(“touch”,common)[/lua] [import]uid: 24216 topic_id: 5732 reply_id: 21684[/import]

You can use a table for your listener instead of a function:
http://developer.anscamobile.com/content/application-programming-guide-event-handling#Listeners_and_Event_Delivery

Then make sure to have a function called “touch” in the table. Now you can stick whatever values you want in the table and they will be accessible in the function:

[code]
local t = {}
function t:touch(event)
print(self.val)
end

t.val = 2
button:addEventListener(“touch”, t)
[/code] [import]uid: 12108 topic_id: 5732 reply_id: 21696[/import]

Hi Flyer,

Thankx for your reply its too helpfull for me.

But I want to Use User define function but i m getting error when i am call the function here I am giving you my code sample :
local common = function(event)
local rec = event.target – the object that triggerd the event
xpos = rec.GblRectPosX
ypos = rec.GblRectPosY

rec.text = “Y”
CheckForCount() – User Define Function

end

– Function for check the value
local function CheckForCount()
Print(TRectangle1.text)
end

TRectangle1.GblRectPosX = xx
TRectangle1.GblRectPosY = yy
TRectangle1.text = “Hello”
TRectangle1:addEventListener(“touch”,common)

TRectangle2.GblRectPosX = xx
TRectangle2.GblRectPosY = yy

TRectangle2:addEventListener(“touch”,common)

TRectangle3.GblRectPosX = xx
TRectangle3.GblRectPosY = yy

TRectangle3:addEventListener(“touch”,common)
is It OK…or give me way where I am going to wrong…

Thankx
Krunal [import]uid: 33757 topic_id: 5732 reply_id: 21925[/import]