Does your UI buttons trigger twice?

Hi
I am on some way a beginner in this arena but I thought I would share this if anyone else does the same mistake and just copy and paste some code regarding UI buttons.

take a look at the below code

[lua]local menuButton = ui.newButton {
defaultSrc = “BTN_mainmenu.png” , defaultX = “142” , defaultY = “52”,
overSrc = “BTN_mainmenu_clicked.png” , overX = “142”, overY = “52”,
onEvent = menuHandler,
id = “menuButton”
}[/lua]

When you insert that code into your game or app it will trigger twice when clicked upon.

If you instead use onRelease like below

[lua]local menuButton = ui.newButton {
defaultSrc = “BTN_mainmenu.png” , defaultX = “142” , defaultY = “52”,
overSrc = “BTN_mainmenu_clicked.png” , overX = “142”, overY = “52”,
onRelease = menuHandler,
id = “menuButton”
}[/lua]

It will only trigger one time and not twice.

This might be the crappiest post today but since I have been having trouble with it, someone else might as well so I though I would share. [import]uid: 22737 topic_id: 11463 reply_id: 311463[/import]

The event in question has both a press and release part. Take a look at the event parameter passed to your function to see. I believe that code uses a custom event, but I imagine it’s like ‘touch’:

http://developer.anscamobile.com/reference/index/eventphase-0 [import]uid: 21712 topic_id: 11463 reply_id: 41565[/import]

TheOddLinquist is right, the button is being pressed twice because the function is being called twice. Once at began and then again at ended.

Try this out:

if event.phase == "began" then  
--Do Something  
end  

[import]uid: 14218 topic_id: 11463 reply_id: 41718[/import]

Yes, I discovered that and that’s why I thought of just trigger the onRelease in the buttoncode at once so I don’t have to check in what state the event is. [import]uid: 22737 topic_id: 11463 reply_id: 41723[/import]