Disabling and Enabling Buttons on SceneLoad

Hi guys, i’m trying to make a button visible on all pages by putting it in my main.lua but the issue i’m having is that on some scenes I want my button to be disabled but when a scene loads where I want the use of the button it becomes enabled. I’ve tried using some things like [lua]myButton:setEnabled(true)[/lua] but cannot find a method of changing it when the scene loads I thought maybe something to do with using myData.lua to tell each scene what to do. So each scene will tell a value to be true or false in myData.lua and my button in main.lua will be disable if the myData.lua is false and enabled if true. However, I don’t know of a way where I can make my button do this, I don’t know how to make the code for this. If anyone knows of a method I would really appreciate it thanks again.

Matt.

Hi Matt,

Consider using a Composer variable (composer.setVariable(), composer.getVariable()) as a pointer/reference to the button. Then, in each scene, just use that reference along with :setEnabled() and it should activate/deactivate the button as needed.

Take care,

Brent

Hi Brent,

I have managed to get my variables to “set” false on my first scene and true on my second, i know this is working as I have it printing in the console when the scene changes. My issue is that my button stays disabled. I have put it in my main.lua so that it is on all pages, and wrote it like this…

[lua]

    local btnHome = widget.newButton

    {

        width = 320,

        height = 50,

        defaultFile = “home.png”,

        onEvent = home,

    }

    btnHome.x = 160

    btnHome.y = 499

    btnHome:setEnabled( composer.getVariable( “btnDisabled” ) )

[/lua]

Any ideas, thanks again.

Matt.

Hi Matt,

I was thinking more like creating a reference to the button like this (when you create it):

[lua]

composer.setVariable( “reference_btnHome”, btnHome )

[/lua]

Then, in another scene:

[lua]

local btn = composer.getVariable( “reference_btnHome” )

btn:setEnabled( false )

–OR

btn:setEnabled( true )

[/lua]

Best regards,

Brent

Hi Brent,

I gave this a go and I got an error.

Runtime error

c:\users\matt\documents\corona projects\hs app load\home.lua:48: attempt to index local ‘btn’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\hs app load\home.lua:48: in function <c:\users\matt\documents\corona projects\hs app load\home.lua:17>

?: in function ‘dispatchEvent’

?: in function ‘gotoScene’

c:\users\matt\documents\corona projects\hs app load\main.lua:4: in main chunk

Any ideas?

Thanks again,

Matt.

Hi Matt,

I don’t know… ensure that all of your variable names and references match up. There’s no obvious reason why this shouldn’t be working.

Brent

Hi Matt,

Consider using a Composer variable (composer.setVariable(), composer.getVariable()) as a pointer/reference to the button. Then, in each scene, just use that reference along with :setEnabled() and it should activate/deactivate the button as needed.

Take care,

Brent

Hi Brent,

I have managed to get my variables to “set” false on my first scene and true on my second, i know this is working as I have it printing in the console when the scene changes. My issue is that my button stays disabled. I have put it in my main.lua so that it is on all pages, and wrote it like this…

[lua]

    local btnHome = widget.newButton

    {

        width = 320,

        height = 50,

        defaultFile = “home.png”,

        onEvent = home,

    }

    btnHome.x = 160

    btnHome.y = 499

    btnHome:setEnabled( composer.getVariable( “btnDisabled” ) )

[/lua]

Any ideas, thanks again.

Matt.

Hi Matt,

I was thinking more like creating a reference to the button like this (when you create it):

[lua]

composer.setVariable( “reference_btnHome”, btnHome )

[/lua]

Then, in another scene:

[lua]

local btn = composer.getVariable( “reference_btnHome” )

btn:setEnabled( false )

–OR

btn:setEnabled( true )

[/lua]

Best regards,

Brent

Hi Brent,

I gave this a go and I got an error.

Runtime error

c:\users\matt\documents\corona projects\hs app load\home.lua:48: attempt to index local ‘btn’ (a nil value)

stack traceback:

c:\users\matt\documents\corona projects\hs app load\home.lua:48: in function <c:\users\matt\documents\corona projects\hs app load\home.lua:17>

?: in function ‘dispatchEvent’

?: in function ‘gotoScene’

c:\users\matt\documents\corona projects\hs app load\main.lua:4: in main chunk

Any ideas?

Thanks again,

Matt.

Hi Matt,

I don’t know… ensure that all of your variable names and references match up. There’s no obvious reason why this shouldn’t be working.

Brent