Getting button state

Hey guys

Is there anyway I can retrieve a button’s current state (whether it is being pressed or not?) from completely unrelated part of my code?

I wasn’t able to find it in the docs, but it seems fairly obvious that there is or should be a way to do that…

Thanks! [import]uid: 151732 topic_id: 33968 reply_id: 333968[/import]

When the button’s change functions occur, set a variable that you can access that has it’s state.
[import]uid: 199310 topic_id: 33968 reply_id: 135053[/import]

Yeah, that was what I was doing, but if you touch a button and release the touch outside of this button bounds, that touch event won’t trigger, meaning my isButtonBeingPressed variable would stay true, unfortunately.

I would really like to hear more ideas/solutions/methods on how to do this.

Thanks [import]uid: 151732 topic_id: 33968 reply_id: 135058[/import]

Can’t you make the button a global (no local in front) and then use the lister to detect it?

function onButtonRelease( event ) local btn = event.target if btn.id == "bMyButton" then [do something...] end end [import]uid: 64538 topic_id: 33968 reply_id: 135193[/import]

Hmmm, perhaps I should explain what I want to do…

I want to move a character WHILE buttonX is being pressed and not only when the button is released or pressed for the first time…

Any ideas?

(please note that a new variable wouldn´t help as if the player presses a button and releases outside the button bounds the button event wouldnt trigger)

[import]uid: 151732 topic_id: 33968 reply_id: 135226[/import]

You can look at some of the joystick code in the Community code to get an idea of how to do that. Or if you want to touch the character and drag it, that’s the other option.

[import]uid: 199310 topic_id: 33968 reply_id: 135251[/import]

When the button’s change functions occur, set a variable that you can access that has it’s state.
[import]uid: 199310 topic_id: 33968 reply_id: 135053[/import]

Yeah, that was what I was doing, but if you touch a button and release the touch outside of this button bounds, that touch event won’t trigger, meaning my isButtonBeingPressed variable would stay true, unfortunately.

I would really like to hear more ideas/solutions/methods on how to do this.

Thanks [import]uid: 151732 topic_id: 33968 reply_id: 135058[/import]

Hi, guilovsh

I have two suggestions.

I would look into the setFocus function, which might help you.

For ordinary display objects you have the canceled phase, but that may not be applicable to buttons in the widget library. [import]uid: 25729 topic_id: 33968 reply_id: 135423[/import]

@Rob Miracle
The thing is, what I really need are 2 different buttons (left and right) and while one of them is being pressed, my character would move. It really seems to me that there should be a way to get the state of a button in the widget library.

@jensto
I tried setting the focus to the pressed button, but that still didn’t make it trigger its event once the mouse went outside of its bounds. Besides, I assume a button from the widget library is already focused once it’s pressed.

And no, these buttons don’t seem to have the canceled phase.


Thanks for trying to help, guys [import]uid: 151732 topic_id: 33968 reply_id: 135477[/import]

An alternative idea might be to just use an image object and attach a listener to it, would that work? [import]uid: 64538 topic_id: 33968 reply_id: 135478[/import]

@gbailey
Yeah, unfortunately it seems to be the only way.
Thanks gbailey :slight_smile: [import]uid: 151732 topic_id: 33968 reply_id: 135484[/import]

Another thought might be, while Corona Labs is re-working their widgets, would be to try out Widget Candy by x-pressive.com. I’ve not used them, but I have heard good things about them.

http://www.x-pressive.com/WidgetCandy_Corona/features.html

Best of luck, let me know how it works out if you do use it.

Happy Holidays! [import]uid: 64538 topic_id: 33968 reply_id: 135486[/import]

Can’t you make the button a global (no local in front) and then use the lister to detect it?

function onButtonRelease( event ) local btn = event.target if btn.id == "bMyButton" then [do something...] end end [import]uid: 64538 topic_id: 33968 reply_id: 135193[/import]

Hmmm, perhaps I should explain what I want to do…

I want to move a character WHILE buttonX is being pressed and not only when the button is released or pressed for the first time…

Any ideas?

(please note that a new variable wouldn´t help as if the player presses a button and releases outside the button bounds the button event wouldnt trigger)

[import]uid: 151732 topic_id: 33968 reply_id: 135226[/import]

You can look at some of the joystick code in the Community code to get an idea of how to do that. Or if you want to touch the character and drag it, that’s the other option.

[import]uid: 199310 topic_id: 33968 reply_id: 135251[/import]

Hi, guilovsh

I have two suggestions.

I would look into the setFocus function, which might help you.

For ordinary display objects you have the canceled phase, but that may not be applicable to buttons in the widget library. [import]uid: 25729 topic_id: 33968 reply_id: 135423[/import]

@Rob Miracle
The thing is, what I really need are 2 different buttons (left and right) and while one of them is being pressed, my character would move. It really seems to me that there should be a way to get the state of a button in the widget library.

@jensto
I tried setting the focus to the pressed button, but that still didn’t make it trigger its event once the mouse went outside of its bounds. Besides, I assume a button from the widget library is already focused once it’s pressed.

And no, these buttons don’t seem to have the canceled phase.


Thanks for trying to help, guys [import]uid: 151732 topic_id: 33968 reply_id: 135477[/import]

An alternative idea might be to just use an image object and attach a listener to it, would that work? [import]uid: 64538 topic_id: 33968 reply_id: 135478[/import]

@gbailey
Yeah, unfortunately it seems to be the only way.
Thanks gbailey :slight_smile: [import]uid: 151732 topic_id: 33968 reply_id: 135484[/import]