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]