I’m trying to use a loop to create a series of buttons that are used for selecting levels.
I’m having problems using one touch handler to determine which button was pressed.
Any help appreciated
[lua]local onLevelTouch = function( event )
if event.phase == “release” then
audio.play( tapSound )
–audio.stop( backgroundSound )
–audio.dispose( backgroundSound ); backgroundSound = nil
if event.target.name == “level1button” then
director:changeScene( “loadlevel1” )
end
if event.target.name == “level2button” then
director:changeScene( “loadlevel2” )
end
end
end --onLevelTouch
local levels = 5
local level = 0
while level < levels do
actualLevel = level + 1
local levelText = “level”…actualLevel…“button”
local levelButton
levelButton = ui.newButton{
defaultSrc = “images/stage1btn.png”,
defaultX = 50,
defaultY = 50,
overSrc = “images/stage1btn-over.png”,
overX = 50,
overY = 50,
onEvent = onLevelTouch,
name = levelText,
text = “”,
font = “Helvetica”,
textColor = { 255, 255, 255, 255 },
size = 16,
emboss = false
}
levelButton:addEventListener( “touch”, levelButton )
levelButton.x = 128+(level*55) levelButton.y = 180
levelButton.isVisible = false
menuGroup:insert( levelButton )
timer.performWithDelay( 200, function() levelButton.isVisible = true; end, 1 )
level = level + 1
end[/lua] [import]uid: 63413 topic_id: 11302 reply_id: 311302[/import]