Tables and listeners

Hello everyone
I have a nice noob question for smart folk like you)
i have this:
[lua]x= -30

local circles = {“coffee1”, “coffee2”, “coffee3”}

function pri()
print(“wow”)
end

for i=1, #circles do
x = x +100

images = display.newImage(circles[i]…".png")
images.x = x
end[/lua]

How to attach different functions to objects(images)? [import]uid: 16142 topic_id: 13854 reply_id: 313854[/import]

you mean different function for different images ?

you can achieve the same with the something like the code below
[lua]x= -30

local circles = {{“coffee1”,“fn1”}, {“coffee2”,“fn2”}, {“coffee3”,“fn3”}}

function pri(event)
print(event.target.tag)
end

for i=1, #circles do
x = x +100
images = display.newImage(circles[i][1]…".png")
images.x = x
images.tag = circles[i][2]
images:addEventListener(“tap”,pri)
end[/lua] [import]uid: 71210 topic_id: 13854 reply_id: 50929[/import]

yeah, different functions to different images
thanks for code, but i dont think this is something i need(
i need to press at first image and it will do something and if i press second or third it will do completely different things
[import]uid: 16142 topic_id: 13854 reply_id: 50947[/import]

yea… you can do it this way
[lua]x= -30

local circles = {{“coffee1”,“fn1”}, {“coffee2”,“fn2”}, {“coffee3”,“fn3”}}

function pri(event)
if event.target.tag == “fn1” then
print(“button 1 code”)
elseif event.target.tag == “fn2” then
print(“button 2 code”)
elseif event.target.tag == “fn3” then
print(“button 1 code”)
end
end

for i=1, #circles do
x = x +100
images = display.newImage(circles[i][1]…".png")
images.x = x
images.tag = circles[i][2]
images:addEventListener(“tap”,pri)
end[/lua] [import]uid: 71210 topic_id: 13854 reply_id: 51018[/import]

or you can set a callback, which is how you have a different function for onComplete in transitions.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13854 reply_id: 51032[/import]

@jayantv is it done this way ?
it doesn’t seems to be working

[lua]x= -30

local circles = {{“coffee1”,“fn1”}, {“coffee2”,“fn2”}, {“coffee3”,“fn3”}}

function fn1(self,event)
print(“wow”)
end

function fn2(self,event)
print(“wow”)
end

function fn3(self,event)
print(“wow”)
end

for i=1, #circles do
x = x +100
images = display.newImage(circles[i][1]…".png")
images.x = x
images.tag = circles[i][2]
images.touch = fn1
images:addEventListener(“touch”,images)
end[/lua] [import]uid: 71210 topic_id: 13854 reply_id: 51036[/import]

it is not as simple as that, just assigning will not call a function, you have to invoke the function too.

so if you set image1.theFunction = fn1

then you need to invoke it as image1.theFunction()

hope that helps you

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13854 reply_id: 51039[/import]

sorry infact the above code i posted will work
i was looking at this
[lua]x= -30

local circles = {{“coffee1”,“fn1”}, {“coffee2”,“fn2”}, {“coffee3”,“fn3”}}

function fn1(self,event)
print(“wow”)
end

function fn2(self,event)
print(“wow”)
end

function fn3(self,event)
print(“wow”)
end

for i=1, #circles do
x = x +100
images = display.newImage(circles[i][1]…".png")
images.x = x
images.tag = circles[i][2]
images.touch = circles[i][2]
images:addEventListener(“touch”,images)
end[/lua] [import]uid: 71210 topic_id: 13854 reply_id: 51040[/import]

and I thik this is how we implement function callback
[lua]x= -30
local circles = {{“cofee1”,“fn1”}, {“cofee2”,“fn2”}, {“cofee3”,“fn3”}}

function fn1(self,event)
print(“wow1”)
end

function fn2(self,event)
print(“wow2”)
end

function fn3(self,event)
print(“wow3”)
end

function newCounter (name)
if name == “fn1” then
return fn1
elseif name == “fn2” then
return fn2
elseif name == “fn3” then
return fn3
end
end

for i=1, #circles do
x = x +100
images = display.newImage(circles[i][1]…".png")
images.x = x
fnname = newCounter(circles[i][2])
images.touch = fnname
images:addEventListener(“touch”,images)
end[/lua] [import]uid: 71210 topic_id: 13854 reply_id: 51042[/import]

found a better way to do this… @Jayant is this what u meant by by callback ?
[lua]x= -30
local circles = {{“cofee1”,“fn1”}, {“cofee2”,“fn2”}, {“cofee3”,“fn3”}}

function fn1(self,event)
print(“wow1”)
end

function fn2(self,event)
print(“wow2”)
end

function fn3(self,event)
print(“wow3”)
end

function newCounter (name)
– this returns a function from the global namespace
return _G[name]
end

for i=1, #circles do
x = x +100
images = display.newImage(circles[i][1]…".png")
images.x = x
– newCounter returns a function
fnname = newCounter(circles[i][2])
images.touch = fnname
images:addEventListener(“touch”,images)
end[/lua] [import]uid: 71210 topic_id: 13854 reply_id: 51220[/import]

not exactly, it is as simple as passing the function to a function as a parameter and then invoking it as in the previous example

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13854 reply_id: 51221[/import]