enemyCnt just keeps track of the total enemies. Then, in the spawnEnemy function it is used to ‘index’ which enemy unit in the enemy table you are referring to as you position the enemy object, assign it an event listener and so forth.
once out of the spawnEnemy function, enemyCnt will be equal to whatever is the total number of enemies.
So, in this example loop, if you have created 15 enemies, enemyCnt will be equal to 15.
In this sample, the loop runs from 1 to 15, and if the enemy has an active transition, it will cancel it.
for i = 1, enemyCnt do if enemytable[i].trans then transition.cancel(enemytable[i].trans) enemytable[i].trans = nil end end
in this sample below, since we are removing display objects (enemy images) and clearing the enemy table, it works best to
loop backwards… So the loop goes from 15, to 1. the -1, tells the loop to subtract 1 each time, so it removes enemy table index #15 first, then 14, then 13 … etc
for i = enemyCnt, 1, -1 do display.remove(enemyTable[i]) enemyTable[i] = nil end enemyTable = nil
I would suggest you can learn a lot of this by checking out some good tutorials and other code other developers have shared. Looking over those tutorials and code and getting to understand them, will really help. But, you should go over several of them. Many indie developers have learned this way. It takes a lot of time, but it is a good way to get a feel for this stuff.
Here are just a few you may want to check out. I have not checked these out, so I am not sure which is the best of them. But you could check each of them out.
http://coronalabs.com/blog/2014/11/04/tutorial-basic-spawning/
http://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/
http://www.christianpeeters.com/complete-app-tutorial/create-a-simple-space-shooter-with-corona-sdk/
http://www.tandgapps.co.uk/resources/tutorial-space-shooter-in-240-lines/
http://www.raywenderlich.com/22064/how-to-make-a-simple-game-with-corona
There are even more then these out there on the internet. You are likely to find even better ones if you search the internet.