Adding event listeners to a table of buttons

Evening fellow Corona geeks. I am stuck, and unable to find an answer to my problem. I am writing up a card type game, and I have created a table of buttons, and I am trying to add an event listener to the table so it will listen for touches on individual objects.

Here is what I have:

local topCardTable = {}
local idx = 0
for vVal = 1, 2 do
for hVal = 1, 4 do
idx = idx + 1
local topCard = display.newImageRect(“assets/UI-Art/NewCard2.png”, 194, 194)
topCard.x = (hVal * 210) + 95
topCard.y = (vVal * 210) - 100
topCard.xScale = 0.90;
topCard.yScale = 0.90;
topCardTable[idx] = topCard
topCardTable[idx].idx = idx
end
end

local function game(event)
if(event.phase == “began”) then
print(“object touched”)
elseif(event.phase == “moved”) then
print(“object moved”)
elseif(event.phase == “ended” or event.phase == “cancelled”) then
print(“object touch ended”)
end
return false;
end

Runtime:addEventListener( “touch”, game )
The runtime listener works great, but now I need to begin adding individual functions or statements to remove buttons upon clicking each one, and what I have above it not working. I think what I need is an event listener and/or function that is listening for actions in each object and this is where I am stuck. [import]uid: 212086 topic_id: 35985 reply_id: 335985[/import]

Hi Hood_Hax78.

You are right. You need to include a listener to the object so you can act on it.

I would add your cards to a group display (let’s say called, cardsGroup) and apply the event listener to each one the group children, using:

for i=1, cardsGroup.numChildren do  
cardsGroup[i]:addEventListener( "touch", game )  
end  

Now, you have a listener to each of your cards.

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35985 reply_id: 143061[/import]

Thanks Renato, this helped out a lot! I knew I was on the right patch, just couldn’t figure out the execution or best way to handle it. [import]uid: 212086 topic_id: 35985 reply_id: 143113[/import]

Hi Hood_Hax78.

You are right. You need to include a listener to the object so you can act on it.

I would add your cards to a group display (let’s say called, cardsGroup) and apply the event listener to each one the group children, using:

for i=1, cardsGroup.numChildren do  
cardsGroup[i]:addEventListener( "touch", game )  
end  

Now, you have a listener to each of your cards.

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35985 reply_id: 143061[/import]

Thanks Renato, this helped out a lot! I knew I was on the right patch, just couldn’t figure out the execution or best way to handle it. [import]uid: 212086 topic_id: 35985 reply_id: 143113[/import]

Hi Hood_Hax78.

You are right. You need to include a listener to the object so you can act on it.

I would add your cards to a group display (let’s say called, cardsGroup) and apply the event listener to each one the group children, using:

for i=1, cardsGroup.numChildren do  
cardsGroup[i]:addEventListener( "touch", game )  
end  

Now, you have a listener to each of your cards.

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35985 reply_id: 143061[/import]

Thanks Renato, this helped out a lot! I knew I was on the right patch, just couldn’t figure out the execution or best way to handle it. [import]uid: 212086 topic_id: 35985 reply_id: 143113[/import]

Hi Hood_Hax78.

You are right. You need to include a listener to the object so you can act on it.

I would add your cards to a group display (let’s say called, cardsGroup) and apply the event listener to each one the group children, using:

for i=1, cardsGroup.numChildren do  
cardsGroup[i]:addEventListener( "touch", game )  
end  

Now, you have a listener to each of your cards.

Renato
Red Beach Games
Try our new game Puzzle You for free here!
[import]uid: 181011 topic_id: 35985 reply_id: 143061[/import]

Thanks Renato, this helped out a lot! I knew I was on the right patch, just couldn’t figure out the execution or best way to handle it. [import]uid: 212086 topic_id: 35985 reply_id: 143113[/import]