Button widget - problem with setEnabled

Hi,

I have an odd problem where disabling a widget button in its onEvent method seem to stop all other events from being called.

For example I have two buttons and one event handler.  I can click button2 as much as i like and the label will change between the over and default colour (there is no onEvent here)

But if I click button1 and it calls letterTouch and therefore setEnabled(false), it changes to the ‘over’ text colour and then freezes.  

I can’t then click button2 or button1 again

I have a Runtime:addEventListener(“enterFrame”, gameLoop) running and that still outputs some debug statements so the app hasnt fully crashed.

Does anybody have any ideas / point out something silly i’ve done / a workaround? 

Thanks

local function letterTouch( event ) if ( event.phase == "began" ) then --code executed when the button is touched event.target:setEnabled(false) end return true end local button = widget.newButton { width = 20, height = 20, id = "let1", label = "1", labelColor = {default = {1,1,1}, over = {204/255,204/255,204/255}}, fontSize = 18, onEvent = letterTouch } button.x = 50 button.y = 50 local button2 = widget.newButton { width = 20, height = 20, id = "let2", label = "2", labelColor = {default = {1,1,1}, over = {204/255,204/255,204/255}}, fontSize = 18, } button2.x = 150 button2.y = 50

local gameLoop = function() print('running') end Runtime:addEventListener("enterFrame", gameLoop)

Hi @david.hunt15,

In my opinion, this is the logical behavior. Since you’re using the “onEvent” handler, which detects all phases of a button interaction, and you’re disabling the button on the “began” phase, it’s expected that it won’t do anything else after that.

I would think this action should be done in either the “ended” phase of the “onEvent” handler, or that you simply use an “onRelease” handler instead, which is effectively the same thing. So, when a user presses the button, it behaves normally, but on the completion of the interaction, it’s disabled.

See the docs for a description of the different handler options:

http://docs.coronalabs.com/api/library/widget/newButton.html

Take care,

Brent

Hey Brent,

As always with the corona staff, I appreciate the quick turn around in response.   :slight_smile:

Ok that kind of makes sense. I’m not at home at the moment but I’ll give it a shot in the ended phase and see if that works.  

Just food for thought… although logical, i would think that disabling the button shouldn’t affect the ‘current’ phase of events that is executing.  Disabling a button to me means the user can’t interact with that button?   infact, in the documentation it says the “set to false the button will not respond to touch event” 

However, this doesn’t explain why it locks out the second button which is unrelated the the first button.  Any ideas on that part of the problem?  
 

Granted, if I move the logic to the ended phase this most likely wont happen but just curious  :slight_smile:

Thanks again for your time and help

Hi David,

I’m not sure why the second button would be reacting in some way to the first. Are you sure there’s not some reference conflict? Do the buttons overlap somehow? Disabling one button shouldn’t affect another…

The code I posted is all that is in the main.lua file and they are named button1 and button2 so no conflict or overlap. Also, button2 has no event handler specified.

If you copy and paste it in to a main.lua it should give you the same problem.

I agree, that one button shouldn’t affect the other but it does in this case.

Hopefully, moving the disable logic to after all phases complete will work around this but there does seem to be some kind of bug here from what i can tell…

Im happy to file a bug report but if you have time can you just double check that I have not done anything stupid… I dont want to waste your engineers time filing a non existent bug  :o

thanks Brent

Hi David,

OK, try to change the code when you have a chance (“phase” and such) and see if it helps. If not, please post the updated code and I’ll see if there’s a legitimate bug.

Brent

Hi brent, 

I tried moving the disabled in to the ended event and that resolves the issue.  Its not ideal though as my logic around what the button actually does is in the began phase as i want it to happen as soon as the button is pressed. Then I have the disable in the ended event.  This is fine in general until the user presses and doesn’t release…  the logic executed due to began phase which is fine but then if i slide my finger off the button and let go, the ended phase is not called

I still this you should be able to disable the button at any phase without it behaving oddly i.e. disabling all but touch events on the screen.

Can you think of a work around to the user moving off the button while touched?

i tried using onPress and having the disabled in there but still the same issue

Hi David,

Personally, I consider a button to be fully operated on its touch and release, but I see you don’t want this behavior (and that’s why we provide the various listener types).

What I would suggest in this somewhat special case is that, on the “began” phase, you set some variable flag (or a property on the button itself) like:

[lua]

button.wasTouchedOnBegan = true

[/lua]

Then, in your listener logic, just say, during the “began” phase, if that property is true, then nothing else will happen.

So effectively, you’re not even using the “setEnabled” function. Instead, you’re building your own functionality around a similar concept.

Brent

thanks brent

Hi @david.hunt15,

In my opinion, this is the logical behavior. Since you’re using the “onEvent” handler, which detects all phases of a button interaction, and you’re disabling the button on the “began” phase, it’s expected that it won’t do anything else after that.

I would think this action should be done in either the “ended” phase of the “onEvent” handler, or that you simply use an “onRelease” handler instead, which is effectively the same thing. So, when a user presses the button, it behaves normally, but on the completion of the interaction, it’s disabled.

See the docs for a description of the different handler options:

http://docs.coronalabs.com/api/library/widget/newButton.html

Take care,

Brent

Hey Brent,

As always with the corona staff, I appreciate the quick turn around in response.   :slight_smile:

Ok that kind of makes sense. I’m not at home at the moment but I’ll give it a shot in the ended phase and see if that works.  

Just food for thought… although logical, i would think that disabling the button shouldn’t affect the ‘current’ phase of events that is executing.  Disabling a button to me means the user can’t interact with that button?   infact, in the documentation it says the “set to false the button will not respond to touch event” 

However, this doesn’t explain why it locks out the second button which is unrelated the the first button.  Any ideas on that part of the problem?  
 

Granted, if I move the logic to the ended phase this most likely wont happen but just curious  :slight_smile:

Thanks again for your time and help

Hi David,

I’m not sure why the second button would be reacting in some way to the first. Are you sure there’s not some reference conflict? Do the buttons overlap somehow? Disabling one button shouldn’t affect another…

The code I posted is all that is in the main.lua file and they are named button1 and button2 so no conflict or overlap. Also, button2 has no event handler specified.

If you copy and paste it in to a main.lua it should give you the same problem.

I agree, that one button shouldn’t affect the other but it does in this case.

Hopefully, moving the disable logic to after all phases complete will work around this but there does seem to be some kind of bug here from what i can tell…

Im happy to file a bug report but if you have time can you just double check that I have not done anything stupid… I dont want to waste your engineers time filing a non existent bug  :o

thanks Brent

Hi David,

OK, try to change the code when you have a chance (“phase” and such) and see if it helps. If not, please post the updated code and I’ll see if there’s a legitimate bug.

Brent

Hi brent, 

I tried moving the disabled in to the ended event and that resolves the issue.  Its not ideal though as my logic around what the button actually does is in the began phase as i want it to happen as soon as the button is pressed. Then I have the disable in the ended event.  This is fine in general until the user presses and doesn’t release…  the logic executed due to began phase which is fine but then if i slide my finger off the button and let go, the ended phase is not called

I still this you should be able to disable the button at any phase without it behaving oddly i.e. disabling all but touch events on the screen.

Can you think of a work around to the user moving off the button while touched?

i tried using onPress and having the disabled in there but still the same issue

Hi David,

Personally, I consider a button to be fully operated on its touch and release, but I see you don’t want this behavior (and that’s why we provide the various listener types).

What I would suggest in this somewhat special case is that, on the “began” phase, you set some variable flag (or a property on the button itself) like:

[lua]

button.wasTouchedOnBegan = true

[/lua]

Then, in your listener logic, just say, during the “began” phase, if that property is true, then nothing else will happen.

So effectively, you’re not even using the “setEnabled” function. Instead, you’re building your own functionality around a similar concept.

Brent