Dynamically generate Event Listeners?

Hi,
I’m testing at the moment corona and it’s really easy to develop with. I so managed to develop a little card came in less than a day. Nevertheless to optimize the code and make it more flexible, some things are like I did them a little too complicated and I didn’t found out how to do them the way I would prefer. To these problems:

I did like I said a little card game where I need a card deck with 20 cards. I make a table with the card names (named cardArray) and inserted them with a loop in a group like this:

for key,file in pairs( cardsArray ) do
singleCard = display.newImage( file,xPos,yPos )
cardBoard:insert( singleCard )
cardBoard[“img”…counter] = singleCard

counter = counter + 1
end

That works so far without problem.

Now I had to apply a Listener to all card to do something with the card if touched. As long as I make an own eventlistener and a own function for all 20 cards like the following it works all fine:

function cardBoardTap1( event )
some code
end

cardBoard.img1:addEventListener( “tap”,cardBoardTap1 )

function cardBoardTap2( event )
some code
end

cardBoard.img2:addEventListener( “tap”,cardBoardTap2 )

function cardBoardTap3( event )
some code
end

cardBoard.img3:addEventListener( “tap”,cardBoardTap3 )
etc.

But that’s everything else than flexible if I want to make different levels with different sums of cards. So it would be a lot more flexible if I could do something like

n=1
while n <= cardNr do
cardBoard[“img”…n]:addEventListener( “tap”,cardBoardTapfunction(n) )
n = n - 1
end

Unfortunately that don’t work. Is there a way to do it this way? And is there a way to give with the Listener a additional variable to the Tap-function to have them like that:

function memoryBoardTap2( event, cardNr )
some code
end

And another question, is there an example somewhere with different screens? I did it in my little game with different functions that initialize new screens and that worked so far, but perhaps there’s a better way? And is there a command to remove all existing Listeners together (I had the problem that by initializing a new screen I had to remove the existing listeners to avoid problems)?

Thanks a lot for your help! [import]uid: 5493 topic_id: 601 reply_id: 300601[/import]

By using event.target you can get to the card being tapped. This way, you can attach the same event handler to all cards and still know which card is being tapped.

Multiple screens can be made by using groups. Make a group for each screen and use group.isVisible = false to hide a group and otherGroup.isVisible = true to get the other group to show.

Cheers, Arjan
[import]uid: 4824 topic_id: 601 reply_id: 1160[/import]

THX Arjen for your help!

Concerning the first part, I already remarked I can get the clicked image by event.target. And at last I reached it by adding a new object property imgID to the image object that I can get then with event.target.imgID. But is there a direct way (without new property) to get the image properties like filename or the key in the cardboard-Array? [import]uid: 5493 topic_id: 601 reply_id: 1163[/import]

I think you really need the extra property (imgID in your case). I tried to inspect the table of an image but couldn’t find any other information that you could use to track back which card it is. [import]uid: 4824 topic_id: 601 reply_id: 1172[/import]