Ok, so I have this philosophy “ALAIW” which means as long as it works. This works during prototyping, but now I am getting towards the end of my complex physics game (Physics and me are best of friends now lol) and I want to clean up the code.
I want to clean it up so I don’t have to go through each…line…and…delete…to change things around.
I have a player, when the player hits a coin it makes a sound and it gets added to the score. That works fine, in fact all this code works perfectly fine. I have 18 coins on any level, however maybe if there is an easy way to “add on” in the future, then that would be great too 
The problem is I want to either centralize this code (maybe make it a module or something ) or just shorten the code doing something like a for loop or something.
After it’s done, anyone can do anything they like with this code, maybe it will help someone else out. 
-Nick
[lua] local function updateCoins(event)
if event.other.myName == “coin1”
then
addToScore()
display.remove( coin1)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin1.x
poof.y = coin1.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
–local poofTimer = timer.performWithDelay( 1000, coinGone, 1 )
–local remover = timer.performWithDelay( 1000, function() display.remove(coin1); end, 1 )
print(“function:updateCoins - Coin1 in your mouth, it’s a party!”)
elseif event.other.myName == “coin2”
then
addToScore()
display.remove( coin2)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin2.x
poof.y = coin2.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin2 in your mouth, it’s a party!”)
elseif event.other.myName == “coin3”
then
addToScore()
display.remove( coin3)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin3.x
poof.y = coin3.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin3 in your mouth, it’s a party!”)
elseif event.other.myName == “coin4”
then
addToScore()
display.remove( coin4)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin4.x
poof.y = coin4.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin4 in your mouth, it’s a party!”)
elseif event.other.myName == “coin5”
then
addToScore()
display.remove( coin5)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin5.x
poof.y = coin5.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin5 in your mouth, it’s a party!”)
elseif event.other.myName == “coin6”
then
addToScore()
display.remove( coin6)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin6.x
poof.y = coin6.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin6 in your mouth, it’s a party!”)
elseif event.other.myName == “coin7”
then
addToScore()
display.remove( coin7)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin7.x
poof.y = coin7.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin7 in your mouth, it’s a party!”)
elseif event.other.myName == “coin8”
then
addToScore()
display.remove( coin8)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin8.x
poof.y = coin8.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin8 in your mouth, it’s a party!”)
elseif event.other.myName == “coin9”
then
addToScore()
display.remove( coin9)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin9.x
poof.y = coin9.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin9 in your mouth, it’s a party!”)
elseif event.other.myName == “coin10”
then
addToScore()
display.remove( coin10)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin10.x
poof.y = coin10.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin10 in your mouth, it’s a party!”)
elseif event.other.myName == “coin11”
then
addToScore()
display.remove( coin11)
local poof1 = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin11.x
poof.y = coin11.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin11 in your mouth, it’s a party!”)
elseif event.other.myName == “coin12”
then
addToScore()
display.remove( coin12)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin12.x
poof.y = coin12.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin12 in your mouth, it’s a party!”)
elseif event.other.myName == “coin13”
then
addToScore()
display.remove( coin13)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin13.x
poof.y = coin13.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin13 in your mouth, it’s a party!”)
elseif event.other.myName == “coin14”
then
addToScore()
display.remove( coin14)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin14.x
poof.y = coin14.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin14 in your mouth, it’s a party!”)
elseif event.other.myName == “coin15”
then
addToScore()
display.remove( coin15)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin15.x
poof.y = coin15.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin15 in your mouth, it’s a party!”)
elseif event.other.myName == “coin16”
then
addToScore()
display.remove( coin16)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin16.x
poof.y = coin16.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin16 in your mouth, it’s a party!”)
elseif event.other.myName == “coin17”
then
addToScore()
display.remove( coin17)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin17.x
poof.y = coin17.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin17 in your mouth, it’s a party!”)
elseif event.other.myName == “coin18”
then
addToScore()
display.remove( coin18)
local poof = display.newImageRect (“images/coin.png”,80,80)
poof.x = coin18.x
poof.y = coin18.y
audio.play(coinCollect)
local remover = timer.performWithDelay( 100, function() display.remove(poof); end, 1 )
print(“function:updateCoins - Coin18 in your mouth, it’s a party!”)
end
end
–ADD EVENT LISTENERS FOR THE PLAYER
player:addEventListener( “collision”, updateCoins )
[/lua] [import]uid: 61600 topic_id: 24279 reply_id: 324279[/import]