[Resolved] passing arguments to a function triggered by an event?

Newbie here! Bear with me…

I have succeeded in creating a tap event with an event listener, and I have confirmed that things are working by printing to the console from within the function that is called by the listener. sorry if I destroyed any terminology there…

In order to create the next chunk of my app, what I think I need to do next is perform some checks within the function that is called when the thing is tapped. However, I cannot figure out how to pass information about the thing that was tapped into this function.

Basically, the user is tapping an image, and after the image is tapped I want to check whether or not the image he tapped was the “right” image. So right now my app displays 3 images, pulled from a table, and tapping any of them will call this one tappedEvent.

Here is the basic code that I’m talking about.

local function tappedEvent (event)  
 print ("something was tapped, this is tappedEvent function")  
 -- would like to check which image was tapped here  
end  
for i=1, 3 do  
 local imgThing = display.newImage( imageTable[i] )  
 imgThing.x = 160,  
 imgThing.y = i\*100+50  
 imgThing:addEventListener( "tap", tappedEvent )  
 localGroup:insert( imgThing )  
end  

Ideally I would like to pass the table index from imageTable that corresponds to the imgThing that was tapped up to the tappedEvent function, so tht I will know which of the 3 images was tapped.

Thanks for any advice! [import]uid: 191855 topic_id: 33751 reply_id: 333751[/import]

Not tested this but…
Assuming you’ve created a display object called imgThing

[code]
local imgThing = display.newImage(…)

– Assign an ‘is correct’ property to the display object
imgThing.isCorrect = true

function imgThing.touch( self, event )
local isCorrect = self.isCorrect
local phase = event.phase
if phase == “began” then
if isCorrect then
print(“Correct”)
else
print(“not correct”)
end
end
end

imgThing:addEventListener(“touch”, imgThing)
[/code] [import]uid: 62617 topic_id: 33751 reply_id: 134145[/import]

There’s an even easier way.

Touch events will have a property called ‘target’ which relates to the object which has called the event.

If you want to know which object was touched you could do the following:

local function tappedEvent (event)  
 print ("something was tapped, this is tappedEvent function")  
 print("This object is object number "..event.target.myIndexThatIJustMadeUp)  
end  
  
for i=1, 3 do  
 local imgThing = display.newImage( imageTable[i] )  
  
 --give the object some kind of unique indentifier, I'll simply use i from the for loop  
 imgThing.myIndexThatIJustMadeUp = i  
 imgThing  
 imgThing.x = 160,  
 imgThing.y = i\*100+50  
 imgThing:addEventListener( "tap", tappedEvent )  
 localGroup:insert( imgThing )  
end  
  

Now if you press your second imgThing you will print “This object is object number 2” [import]uid: 84115 topic_id: 33751 reply_id: 134150[/import]

– [import]uid: 74338 topic_id: 33751 reply_id: 134161[/import]

I was able to make your method work AlanPlantPot! Thanks!!! [import]uid: 191855 topic_id: 33751 reply_id: 134172[/import]

Glad to here that. You can use event.target to trigger functions etc as well, such as using

event.target:removeSelf()  
event.target = nil  

to remove the object. [import]uid: 84115 topic_id: 33751 reply_id: 134174[/import]

Not tested this but…
Assuming you’ve created a display object called imgThing

[code]
local imgThing = display.newImage(…)

– Assign an ‘is correct’ property to the display object
imgThing.isCorrect = true

function imgThing.touch( self, event )
local isCorrect = self.isCorrect
local phase = event.phase
if phase == “began” then
if isCorrect then
print(“Correct”)
else
print(“not correct”)
end
end
end

imgThing:addEventListener(“touch”, imgThing)
[/code] [import]uid: 62617 topic_id: 33751 reply_id: 134145[/import]

There’s an even easier way.

Touch events will have a property called ‘target’ which relates to the object which has called the event.

If you want to know which object was touched you could do the following:

local function tappedEvent (event)  
 print ("something was tapped, this is tappedEvent function")  
 print("This object is object number "..event.target.myIndexThatIJustMadeUp)  
end  
  
for i=1, 3 do  
 local imgThing = display.newImage( imageTable[i] )  
  
 --give the object some kind of unique indentifier, I'll simply use i from the for loop  
 imgThing.myIndexThatIJustMadeUp = i  
 imgThing  
 imgThing.x = 160,  
 imgThing.y = i\*100+50  
 imgThing:addEventListener( "tap", tappedEvent )  
 localGroup:insert( imgThing )  
end  
  

Now if you press your second imgThing you will print “This object is object number 2” [import]uid: 84115 topic_id: 33751 reply_id: 134150[/import]

– [import]uid: 74338 topic_id: 33751 reply_id: 134161[/import]

I was able to make your method work AlanPlantPot! Thanks!!! [import]uid: 191855 topic_id: 33751 reply_id: 134172[/import]

Glad to here that. You can use event.target to trigger functions etc as well, such as using

event.target:removeSelf()  
event.target = nil  

to remove the object. [import]uid: 84115 topic_id: 33751 reply_id: 134174[/import]