Handling button touch events

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]

Change

event.target.name

to

event.name

see if that works

cheers

ade [import]uid: 66324 topic_id: 11302 reply_id: 41041[/import]

Nop don’t work.

I put a line for debugging
[lua]print(“Event=”…event.name)[/lua]

and I got a runtime error message
“attempt to concatenate field ‘name’ (a nil value)” [import]uid: 63413 topic_id: 11302 reply_id: 41109[/import]

Change the button creation to this:

  
 levelButton = ui.newButton{  
 defaultSrc = "images/stage1btn.png",  
 defaultX = 50,  
 defaultY = 50,  
 overSrc = "images/stage1btn-over.png",  
 overX = 50,  
 overY = 50,  
 onEvent = onLevelTouch,  
  
 text = "",  
 font = "Helvetica",  
 textColor = { 255, 255, 255, 255 },  
 size = 16,  
 emboss = false  
 }  
  
 levelButton.name = levelText  
  

Still use event.target.name, not sure if it will work though. [import]uid: 5833 topic_id: 11302 reply_id: 41113[/import]

Ok this is what I did, and it works.

[lua]local onLevelTouch = function( event )
local levelName = event.target.name
print(“Event=”…levelName)
print(“Phase=”…event.phase)
if event.phase == “ended” then
audio.play( tapSound )
–audio.stop( backgroundSound )
–audio.dispose( backgroundSound ); backgroundSound = nil
director:changeScene( “load_”…levelName )
end
end --onLevelTouch

if maxLevels <= 5 then
local levels = maxLevels
local level = 0
while level < levels do
actualLevel = level + 1
local levelText = “stage”…stageNumber…“level”…actualLevel
local levelButton
levelButton = display.newImageRect(“images/stage1btn.png”,50,50)
levelButton.name = levelText
levelButton:addEventListener( “touch”, onLevelTouch )
levelButton.x = 128+(level*55) levelButton.y = 180
levelButton.isVisible = true
menuGroup:insert( levelButton )
level = level + 1
end --while
end – if

hi guys, does anyone here knows how to change the text/label of a button after click/release?

actions[“button2”] = function()
button2.text=“aaa”
end

I’m also searching for ui properties can someone recommend a reference please?

Thankyou! [import]uid: 70581 topic_id: 11302 reply_id: 42456[/import]