change button label on button press, and disable other button events while another button event is occuring

-how do i get the label of my button to change when it is pressed

-i.e i want the label of the button (the current label is “move eye” ) to change to “move back” when the button has beened pressed; and then when the user presses the button again the label goes back to “move eye”;

and then when pressed again it becomes “move back” again and so on…

-also can i make it so that when the user clicks the button (which triggers an object transition event) the button cant be pressed again until either half the transition is over and the other buttons on the screen cant be pressed at all during the *‘whole’; the transition event is in 2 parts so when the button is clicked the image moves and the when the button is pressed again the image moves back

i.e the button is presses, the image moves (and while it is moving the user cant press any buttons while this is happening) and then the user can press the button again and the image moves back to its origin, after which the other buttons on the screen can be pressed.

*note: ‘whole’ transition meaning, the transition when the button is pressed 1st time and the object moves and then the button is pressed again and the object goes back to its origin

here is the code i have so far below:

sorry for being long winded but thats as short as i could think of writing it

answer ASAP plesae & thanks

[lua]local rightEyeCover = display.newImage( “eyecoverright.png”, left, top, isFullResolution )
rightEyeCover:translate( 620, -30)
rightEyeCoverMovement = 1

local function event1(event)
if rightEyeCoverMovement == 1 then
local function finishMove1()
rightEyeCoverMovement = 2
end
rightEyeCoverMovement = 3
transition.to(rightEyeCover, {time = 2000, x = -350, delta=true, transition=easing.outQuad, onComplete=finishMove1 })
end
if rightEyeCoverMovement == 2 then
local function finishMove2()
rightEyeCoverMovement = 1
end

rightEyeCoverMovement = 3
transition.to(rightEyeCover, {time = 2000, x = 350, delta=true, transition=easing.outQuad, onComplete=finishMove2 })
end
end

local button = widget.newButton{
label = “move eye”,
fontSize = 30,
width = 640,
height = 90,
left = 0,
top = 355,
onPress=event1
}

[/lua]

You can change the button’s label using the setLabel() method.  Please see:

http://docs.coronalabs.com/api/type/ButtonWidget/index.html

for more information.  You can use the setEnabled() method to disable the button.  Now the tricky part is re-enabling it in the middle of the transition.  If you wanted to wait on it to complete, you could set up an onCompelete handler that would call setEnabled() to re-enable it when it finished.  But since you know how long your transition is supposed to last, you could use a timer.performWithDelay() call for half of your transition and when the timer finishes, re-enable your button.  The docs for setEnabled() are at the same link above.

thanks alot for the help

however i keep getting error messages whenever i input either of these functions

i,ll post the code below

if i can get these two functions to work my app will be done in no time

local rightEyeCover = display.newImage( "eyecoverright.png", left, top, isFullResolution ) rightEyeCover:translate( 620, -30) rightEyeCoverMovement = 1   local function event1(event)     if rightEyeCoverMovement == 1 then         local function finishMove1()             rightEyeCoverMovement = 2         end     rightEyeCoverMovement = 3     transition.to(rightEyeCover, {time = 2000, x = -350, delta=true, transition=easing.outQuad, onComplete=finishMove1 })     transition.to(eye, {time = 2000, x=-30, delta=true, transition=easing.outQuad, onComplete=finishMove1 })     button2:setEnabled=( false )     button:setLabel( "press again" )     end     if rightEyeCoverMovement == 2 then         local function finishMove2()          rightEyeCoverMovement = 1         end                  rightEyeCoverMovement = 3     transition.to(rightEyeCover, {time = 2000, x = 350, delta=true, transition=easing.outQuad, onComplete=finishMove2 })     transition.to(eye, {time = 2000, x=30, delta=true, transition=easing.outQuad, onComplete=finishMove1 })     button2:setEnabled=( true )     button:setLabel( "move eye" )     end end local button = widget.newButton{     label = "move eye",     fontSize = 30,     width = 640,     height = 90,     left = 0,     top = 355,     onPress=event1 } rightEyeMovement = 1   local function event2(event)     if rightEyeMovement == 1 then         local function finishMove1()             rightEyeMovement = 2         end     rightEyeMovement = 3     transition.to(eye, {time = 2000, x=30, delta=true, transition=easing.outQuad, onComplete=finishMove1 })     end     if rightEyeMovement == 2 then         local function finishMove2()          rightEyeMovement = 1         end                  rightEyeMovement = 3     transition.to(eye, {time = 2000, x=-30, delta=true, transition=easing.outQuad, onComplete=finishMove1 })     end end local button2 = widget.newButton{     label = "remove eye",     fontSize = 30,     width = 640,     height = 90,     left = 0,     top = 445,     onPress=event2 }

heres the two functions i added into the code above:

    button2:setEnabled=( false )     button:setLabel( "press again" )

and

    button2:setEnabled=( true )     button:setLabel( "move eye" )

Hello @dcj_786,

I don’t know the error message you’re receiving, but the correct usage is this:

[lua]button2:setEnabled( false )[/lua]

but not this…

[lua]button2:setEnabled=( false )[/lua]

Best regards,

Brent

thanks alot sir

ive made the corrected the mistake that you pointed out above thanks

the error message i am still getting is as follows:

"c:\users\admin\desktop\hello world\main.lua:51: attempt to index global ‘button2’ (a nil value)

stack traceback:

                          [C]: ?

                           c:\users\admin\desktop\hello world\main.lua:51: in function ‘_onPress’

                           ?:in function ‘?’

                           ?:in function <?:1052>

                           ?:in function <?:218>

"

i can see that there is something wrong with the onPress functon but i cant understand what,

Could you explain to me what the issue is and also if you can could you explain to me how i read these error messages in order to solve the problem

the help is thoroughly appreciated

thanks again

Hi @dcj_786,

This appears to be a basic “scope” error. Scope is one of the most important things to learn in Lua, because it applies to (almost) everything in some way or another. To get started on understanding it, you can view this video:

http://www.youtube.com/watch?v=2ATlcGP2zMY

In fact, I suggest you watch all of the videos under “Corona Basics” in Corona University:

http://www.coronalabs.com/resources/tutorials/corona-basics/

Finally, please read this tutorial on basic debugging:

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Hope this helps,

Brent

Thanks ive just finished watching the videos

ive realised that because i calling for the function ‘event1’ when ‘button’ is pressed, the event1 function must be placed before the code for the widget.newButton. Therefore if i try to place a function in ‘event1’ that disables button 2 i need to place the code for button 2 before the ‘event1’ function

however when if i do this the code for ‘button’ will be below button2’s code, therefore if i try to use a setEnabled button function for ‘button’ in button2’s ‘event2’ function the program wont know what ‘button’ is as it comes afterwards in the code

do i have to input the widget.newButton code chunk first and then call the button onPress Event afterwards?

is there a way around this? most of the videos i have watched show how scope works using ‘print’ which doesnt help me apply the code to my situation

thanks again :slight_smile:

Any help asap would be much appreciated thankyou

You simply need to forward declare things:

local button

local function someFunction() – that manipulates button

      button.x = 100

end

button = widget.newButton(…)

thanks sir, it worked after i foward declared them and i changed the buttons into global variables

thanks alot for the help

You can change the button’s label using the setLabel() method.  Please see:

http://docs.coronalabs.com/api/type/ButtonWidget/index.html

for more information.  You can use the setEnabled() method to disable the button.  Now the tricky part is re-enabling it in the middle of the transition.  If you wanted to wait on it to complete, you could set up an onCompelete handler that would call setEnabled() to re-enable it when it finished.  But since you know how long your transition is supposed to last, you could use a timer.performWithDelay() call for half of your transition and when the timer finishes, re-enable your button.  The docs for setEnabled() are at the same link above.

thanks alot for the help

however i keep getting error messages whenever i input either of these functions

i,ll post the code below

if i can get these two functions to work my app will be done in no time

local rightEyeCover = display.newImage( "eyecoverright.png", left, top, isFullResolution ) rightEyeCover:translate( 620, -30) rightEyeCoverMovement = 1 &nbsp; local function event1(event) &nbsp;&nbsp;&nbsp; if rightEyeCoverMovement == 1 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local function finishMove1() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rightEyeCoverMovement = 2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; rightEyeCoverMovement = 3 &nbsp;&nbsp;&nbsp; transition.to(rightEyeCover, {time = 2000, x = -350, delta=true, transition=easing.outQuad, onComplete=finishMove1 }) &nbsp;&nbsp;&nbsp; transition.to(eye, {time = 2000, x=-30, delta=true, transition=easing.outQuad, onComplete=finishMove1 }) &nbsp;&nbsp;&nbsp; button2:setEnabled=( false ) &nbsp;&nbsp;&nbsp; button:setLabel( "press again" ) &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp;&nbsp; if rightEyeCoverMovement == 2 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local function finishMove2() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rightEyeCoverMovement = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; rightEyeCoverMovement = 3 &nbsp;&nbsp;&nbsp; transition.to(rightEyeCover, {time = 2000, x = 350, delta=true, transition=easing.outQuad, onComplete=finishMove2 }) &nbsp;&nbsp;&nbsp; transition.to(eye, {time = 2000, x=30, delta=true, transition=easing.outQuad, onComplete=finishMove1 }) &nbsp;&nbsp;&nbsp; button2:setEnabled=( true ) &nbsp;&nbsp;&nbsp; button:setLabel( "move eye" ) &nbsp;&nbsp; &nbsp;end end local button = widget.newButton{ &nbsp;&nbsp; &nbsp;label = "move eye", &nbsp;&nbsp; &nbsp;fontSize = 30, &nbsp;&nbsp; &nbsp;width = 640, &nbsp;&nbsp; &nbsp;height = 90, &nbsp;&nbsp; &nbsp;left = 0, &nbsp;&nbsp; &nbsp;top = 355, &nbsp;&nbsp; &nbsp;onPress=event1 } rightEyeMovement = 1 &nbsp; local function event2(event) &nbsp;&nbsp;&nbsp; if rightEyeMovement == 1 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local function finishMove1() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rightEyeMovement = 2 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; rightEyeMovement = 3 &nbsp;&nbsp;&nbsp; transition.to(eye, {time = 2000, x=30, delta=true, transition=easing.outQuad, onComplete=finishMove1 }) &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; if rightEyeMovement == 2 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local function finishMove2() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rightEyeMovement = 1 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; rightEyeMovement = 3 &nbsp;&nbsp;&nbsp; transition.to(eye, {time = 2000, x=-30, delta=true, transition=easing.outQuad, onComplete=finishMove1 }) &nbsp;&nbsp;&nbsp; end end local button2 = widget.newButton{ &nbsp;&nbsp;&nbsp; label = "remove eye", &nbsp;&nbsp;&nbsp; fontSize = 30, &nbsp;&nbsp;&nbsp; width = 640, &nbsp;&nbsp;&nbsp; height = 90, &nbsp;&nbsp;&nbsp; left = 0, &nbsp;&nbsp;&nbsp; top = 445, &nbsp;&nbsp;&nbsp; onPress=event2 }

heres the two functions i added into the code above:

&nbsp;&nbsp;&nbsp; button2:setEnabled=( false ) &nbsp;&nbsp;&nbsp; button:setLabel( "press again" )

and

&nbsp;&nbsp;&nbsp; button2:setEnabled=( true ) &nbsp;&nbsp;&nbsp; button:setLabel( "move eye" )

Hello @dcj_786,

I don’t know the error message you’re receiving, but the correct usage is this:

[lua]button2:setEnabled( false )[/lua]

but not this…

[lua]button2:setEnabled=( false )[/lua]

Best regards,

Brent

thanks alot sir

ive made the corrected the mistake that you pointed out above thanks

the error message i am still getting is as follows:

"c:\users\admin\desktop\hello world\main.lua:51: attempt to index global ‘button2’ (a nil value)

stack traceback:

                          [C]: ?

                           c:\users\admin\desktop\hello world\main.lua:51: in function ‘_onPress’

                           ?:in function ‘?’

                           ?:in function <?:1052>

                           ?:in function <?:218>

"

i can see that there is something wrong with the onPress functon but i cant understand what,

Could you explain to me what the issue is and also if you can could you explain to me how i read these error messages in order to solve the problem

the help is thoroughly appreciated

thanks again

Hi @dcj_786,

This appears to be a basic “scope” error. Scope is one of the most important things to learn in Lua, because it applies to (almost) everything in some way or another. To get started on understanding it, you can view this video:

http://www.youtube.com/watch?v=2ATlcGP2zMY

In fact, I suggest you watch all of the videos under “Corona Basics” in Corona University:

http://www.coronalabs.com/resources/tutorials/corona-basics/

Finally, please read this tutorial on basic debugging:

http://www.coronalabs.com/blog/2013/07/09/tutorial-basic-debugging/

Hope this helps,

Brent

Thanks ive just finished watching the videos

ive realised that because i calling for the function ‘event1’ when ‘button’ is pressed, the event1 function must be placed before the code for the widget.newButton. Therefore if i try to place a function in ‘event1’ that disables button 2 i need to place the code for button 2 before the ‘event1’ function

however when if i do this the code for ‘button’ will be below button2’s code, therefore if i try to use a setEnabled button function for ‘button’ in button2’s ‘event2’ function the program wont know what ‘button’ is as it comes afterwards in the code

do i have to input the widget.newButton code chunk first and then call the button onPress Event afterwards?

is there a way around this? most of the videos i have watched show how scope works using ‘print’ which doesnt help me apply the code to my situation

thanks again :slight_smile:

Any help asap would be much appreciated thankyou

You simply need to forward declare things:

local button

local function someFunction() – that manipulates button

      button.x = 100

end

button = widget.newButton(…)

thanks sir, it worked after i foward declared them and i changed the buttons into global variables

thanks alot for the help