Multiple functions in ones !

I’m making an app where you have multiple levels and i am doing a Listener for each level but I think it is not a method very efficient, becasue I repeat the same function for each level, varying only in the numbering. Here attached a part. any suggestions?

function startGame(event)
menuPrincipal.grupo.alpha=0
niveles.grupo.alpha=0
core1.grupoAlerta.alpha=0
core1.grupo.alpha=1
return true
end
function startGame2(event)
menuPrincipal.grupo.alpha=0
niveles.grupo.alpha=0
core2.grupoAlerta.alpha=0
core2.grupo.alpha=1
return true
end
function startGame3(event)
menuPrincipal.grupo.alpha=0
niveles.grupo.alpha=0
core3.grupoAlerta.alpha=0
core3.grupo.alpha=1
return true
end
function startGame4(event)
menuPrincipal.grupo.alpha=0
niveles.grupo.alpha=0
core4.grupoAlerta.alpha = 0
core4.grupo.alpha = 1
return true
end

niveles.nivel1:addEventListener(“tap”,startGame)
niveles.nivel2:addEventListener(“tap”,startGame2)
niveles.nivel3:addEventListener(“tap”,startGame3)
niveles.nivel4:addEventListener(“tap”,startGame4) [import]uid: 180288 topic_id: 35083 reply_id: 335083[/import]

Please use < code > < / code > around your code snippets when posting (minus the spaces) so that it appears like legible code, thanks :slight_smile:

What you should probably do here is use tables! :slight_smile:

[code] – Step 1
– Instead of “core1”, “core2”, etc. use:
local core = {}
core[1] = display.newImage() – or whatever command you use to create “core1”
– then just repeat for each one, core[2], core[3], etc!

– Step 2
– when you make your buttons, give each one a number id!
core[2].id = 2 – that sort of thing
– Step 3
– make a single function!
function startGame(event)
if event.target.id then
core[event.target.id].grupoAlerta.alpha = 0
– etc
else
print(“no event id!”)
end
end[/code] [import]uid: 41884 topic_id: 35083 reply_id: 139509[/import]

Give each of your buttons an id number, i.e. button1.id =1, button2.id =2 etc

Give all the buttons the same event listnener.

Within your listener, get the id of the button pressed via event.target.id and use that to load the appropriate level. [import]uid: 93133 topic_id: 35083 reply_id: 139511[/import]

Please use < code > < / code > around your code snippets when posting (minus the spaces) so that it appears like legible code, thanks :slight_smile:

What you should probably do here is use tables! :slight_smile:

[code] – Step 1
– Instead of “core1”, “core2”, etc. use:
local core = {}
core[1] = display.newImage() – or whatever command you use to create “core1”
– then just repeat for each one, core[2], core[3], etc!

– Step 2
– when you make your buttons, give each one a number id!
core[2].id = 2 – that sort of thing
– Step 3
– make a single function!
function startGame(event)
if event.target.id then
core[event.target.id].grupoAlerta.alpha = 0
– etc
else
print(“no event id!”)
end
end[/code] [import]uid: 41884 topic_id: 35083 reply_id: 139509[/import]

Give each of your buttons an id number, i.e. button1.id =1, button2.id =2 etc

Give all the buttons the same event listnener.

Within your listener, get the id of the button pressed via event.target.id and use that to load the appropriate level. [import]uid: 93133 topic_id: 35083 reply_id: 139511[/import]